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

这里写目录标题

定义接口

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);
    }
}
相关推荐
Sahadev_1 天前
macOS 解决 AirDrop 传输应用“已损坏“问题,以sublime为例
macos·策略模式·sublime text
筱璦2 天前
期货软件开发「启动加载页 / 初始化窗口」
前端·c#·策略模式·期货
喵叔哟2 天前
2.【.NET10 实战--孢子记账--产品智能化】--升级前的准备工作:项目依赖梳理与升级计划制定
.net·策略模式
qq_232045573 天前
精积微半导体面试(部分)
netty·策略模式·nio·内存抖动·threadlocal·bitmap·复用
badhope4 天前
OpenClaw卸载命令全解析
java·linux·人工智能·python·sql·数据挖掘·策略模式
蜜獾云4 天前
设计模式之策略模式:替换掉糟糕的if else语句实现面向对象编程而非面向过程
设计模式·策略模式
kishu_iOS&AI4 天前
OpenClaw 管理 API Key / Token 的常见安全方案
安全·ai·策略模式·openclaw
ckm紫韵5 天前
OpenShift CLI (oc)客户端安装以及常用命令
策略模式·openshift·oc
C+++Python5 天前
C++ 策略模式实战:从原理到落地
开发语言·c++·策略模式
CS创新实验室5 天前
从硬件抽象到意图对齐:论 AI 时代操作系统演进的逻辑必然与 OpenClaw 的范式价值
人工智能·操作系统·策略模式