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

这里写目录标题

定义接口

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);
    }
}
相关推荐
守城小轩10 天前
Chromium 136 编译指南 macOS篇:编译优化技巧(六)
macos·策略模式
尤物程序猿10 天前
设计模式之手写策略模式实现动态支付(Java实现)
java·设计模式·策略模式
守城小轩15 天前
Chromium 136 编译指南 macOS篇:Xcode安装与配置(二)
macos·xcode·策略模式
秋田君19 天前
深入理解JavaScript设计模式之策略模式
javascript·设计模式·策略模式
lpfasd12320 天前
策略模式(Strategy Pattern)
策略模式
南玖yy21 天前
深入理解 x86 汇编中的符号扩展指令:从 CBW 到 CDQ 的全解析
开发语言·汇编·arm开发·后端·架构·策略模式
电控极客21 天前
电动汽车驱动模式扭矩控制设计方法
经验分享·算法·汽车·策略模式
江梦寻25 天前
MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)
开发语言·后端·python·macos·架构·策略模式
南玖yy1 个月前
x86 汇编逻辑运算全解析:从【位操作】到实际应用(AND,OR,NOT,XOR,TEST)
开发语言·汇编·arm开发·后端·架构·策略模式
张伯毅1 个月前
Flink 失败重试策略 :restart-strategy.type
大数据·flink·策略模式