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

这里写目录标题

定义接口

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);
    }
}
相关推荐
_OP_CHEN18 小时前
【Linux系统编程】(四十五)线程池基础:日志系统设计与策略模式的优雅落地
linux·操作系统·线程池·进程·策略模式·c/c++·日志系统
像少年啦飞驰点、21 小时前
Java策略模式从入门到实战:小白也能看懂的设计模式指南
java·设计模式·策略模式·编程入门·小白教程
乌萨奇也要立志学C++1 天前
【Linux】线程池(一)C++ 手写线程池:基于策略模式实现高性能日志模块
linux·c++·策略模式
JTCC1 天前
Java 设计模式西游篇 - 第三回:策略模式换法宝 三打白骨精变招
java·设计模式·策略模式
头发还在的女程序员15 天前
【免费下载】企业能源管理系统
小程序·策略模式·能源管理
前端 贾公子15 天前
React 和 Vue 都离不开的表单验证库 async-validator 之策略模式的应用 (上)
vue.js·react.js·策略模式
小米49616 天前
Js设计模式---策略模式
设计模式·策略模式
geovindu16 天前
python: Strategy Pattern
python·设计模式·策略模式
Hello.Reader19 天前
Flink 任务失败恢复机制Restart Strategy 和 Failover Strategy 怎么配才“又稳又不炸”
大数据·flink·策略模式
武帝为此21 天前
【Linux strace命令介绍】
linux·运维·策略模式