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

这里写目录标题

定义接口

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);
    }
}
相关推荐
有个人神神叨叨35 分钟前
AITalk:从零到一打造 macOS 系统级语音输入引擎
macos·策略模式
且去填词1 天前
DeepSeek :基于 AST 与 AI 的遗留系统“手术刀”式治理方案
人工智能·自动化·llm·ast·agent·策略模式·deepseek
Byron Loong2 天前
【系统】Mac系统和Linux 指令对比
linux·macos·策略模式
Coder码匠2 天前
策略模式的实际应用:从单一数据源到多数据源架构
java·架构·策略模式
星河耀银海3 天前
C++开发入门——环境搭建与第一个程序
开发语言·c++·策略模式
__万波__3 天前
二十三种设计模式(二十二)--策略模式
java·设计模式·策略模式
rgeshfgreh4 天前
C++模板与ABI:深度解析参数传递
策略模式
FreeBuf_5 天前
新型TCC绕过漏洞:macOS面临自动化攻击风险
macos·自动化·策略模式
JavaBoy_XJ6 天前
行为型-策略模式
策略模式