工厂模式加策略模式 -- 具体实现

这里写目录标题

定义接口

java 复制代码
public interface EntityHandler extends InitializingBean {
    MatchContentDTO match(MatchEntityDTO matchEntityDTO);
    String supportEntityType();
}

定义抽象类

java 复制代码
public abstract class AbstractEntityHandler implements EntityHandler {
    @Override
    public final MatchContentDTO match(MatchEntityDTO matchEntityDTO) {
        EntityHandler specialMatchHandler = getSpecialMatchHandler(matchEntityDTO);
        if (specialMatchHandler != null) {
            return specialMatchHandler.match(matchEntityDTO);
        }
        return doCommonMatch(matchEntityDTO);
    }


    public abstract MatchContentDTO doCommonMatch(MatchEntityDTO matchEntityDTO);


    public EntityHandler getSpecialMatchHandler(MatchEntityDTO matchEntityDTO) {
        return null;
    }

}

定义主处理器

java 复制代码
@Service
@Slf4j
public class EntitySystemHandler extends AbstractEntityHandler {

    @Override
    public MatchContentDTO doCommonMatch(MatchEntityDTO matchEntityDTO) {
        log.info("EntitySystemHandler:{}", JSON.toJSONString(matchEntityDTO));
        return new MatchContentDTO();
    }

    @Override
    public String supportEntityType() {
        return this.getClass().getName();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        EntityFactory.register(this);
    }

    @Override
    public EntityHandler getSpecialMatchHandler(MatchEntityDTO matchEntityDTO) {
        return super.getSpecialMatchHandler(matchEntityDTO);
    }
}

分支处理器

java 复制代码
@Service
@Slf4j
public class TimeEntityHandler extends AbstractEntityHandler {

    @Override
    public String supportEntityType() {
        return  this.getClass().getName();
    }

    @Override
    public MatchContentDTO doCommonMatch(MatchEntityDTO matchEntityDTO) {
        log.info("TimeEntityHandler:{}", JSON.toJSONString(matchEntityDTO));
        return new MatchContentDTO();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        EntityFactory.register(this);
    }
}

定义工厂

java 复制代码
public class EntityFactory{

    private static final Map<String, EntityHandler> ENTITY_HANDLER_MAP = new HashMap<>();

    public static EntityHandler getEntityHandler(String entityType){
        return ENTITY_HANDLER_MAP.get(entityType);
    }

    public static void register(EntityHandler handler) {
        if (handler == null) {
            return;
        }
        ENTITY_HANDLER_MAP.put(handler.supportEntityType(), handler);
    }

}

demo

java 复制代码
@SpringBootTest
@RunWith(SpringRunner.class)
public class EntityTest {

    @Test
    public  void  getTest(){
        EntityHandler entityHandler = EntityFactory.getEntityHandler("system");
        MatchEntityDTO matchEntityDTO = new MatchEntityDTO();
        entityHandler.match(matchEntityDTO);
    }
}
相关推荐
赶路人儿3 天前
MacOS 下实现 AI 操控电脑(Computer Use)的思考
人工智能·macos·策略模式
Fozei3 天前
录屏原理与鼠标可视化
计算机外设·策略模式
AIex-YH7 天前
菌种工艺四讲(三):育种工具不是“越新越好“,是“越匹配越好“
制造·策略模式
起司喵喵7 天前
Flutter-MacOS桌面OS系统|flutter.+window_manager客户端OS模板
flutter·macos·策略模式
啊真真真7 天前
macOS下libnfc ..写卡失败问题及解决方案
macos·策略模式
Eric Woo X9 天前
macOS 旧版本无法启动 Steam ?一条命令恢复运行(2026年7月实测)
macos·策略模式
张小姐的猫17 天前
【Linux】网络编程 —— HTTP协议(上)
linux·运维·服务器·网络·http·单例模式·策略模式
笨拙的老猴子23 天前
小明学架构师的第七课:策略模式:告别if-else的选择困难症
系统架构·策略模式
肖爱Kun24 天前
C++设计策略模式
开发语言·c++·策略模式
折哥的程序人生 · 物流技术专研25 天前
Java 23 种设计模式:从踩坑到精通 | 番外:策略 vs 模板方法 —— 组合与继承的终极对决
java·策略模式·java面试·comparator·模版方法模式·java设计模式·从踩坑到精通