当前位置: 首页 > article >正文

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

这里写目录标题

      • 定义接口
      • 定义抽象类
      • 定义主处理器
      • 分支处理器
      • 定义工厂
      • demo

在这里插入图片描述

定义接口

public interface EntityHandler extends InitializingBean {
    MatchContentDTO match(MatchEntityDTO matchEntityDTO);
    String supportEntityType();
}

定义抽象类

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;
    }

}

定义主处理器


@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);
    }
}

分支处理器

@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);
    }
}

定义工厂

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


@SpringBootTest
@RunWith(SpringRunner.class)
public class EntityTest {

    @Test
    public  void  getTest(){
        EntityHandler entityHandler = EntityFactory.getEntityHandler("system");
        MatchEntityDTO matchEntityDTO = new MatchEntityDTO();
        entityHandler.match(matchEntityDTO);
    }
}

在这里插入图片描述


http://www.kler.cn/a/580494.html

相关文章:

  • AI巨浪中的安全之舵:天空卫士助力人工智能落地远航
  • OpenManus介绍及本地部署体验
  • DeepSeek-Open WebUI部署
  • 多线程--参数传递之间的关系
  • react中字段响应式
  • springboot3整合knife4j详细版,包会!(不带swagger2玩)
  • 没有与此调用匹配的重载
  • 量子计算对区块链技术的影响:革新与挑战
  • 开源项目ESP-SparkBot: ESP32-S3 大模型 AI 桌面机器人(复刻分享)
  • 服务端和客户端通信(TCP)
  • OpenAI Whisper:开启语音转文本的智能时代
  • Unity DOTS 从入门到精通之传统 Unity 设计转换为 DOTS 设计
  • 驾培市场与低空经济无人机融合技术详解
  • HTML 表单 (form) 的作用解释
  • 【C++设计模式】第三篇:抽象工厂模式(Abstract Factory)
  • 行为模式---中介者模式
  • MATLAB中movsum函数用法
  • 使用AI一步一步实现若依前端(5)
  • Java直通车系列25【Spring Boot】(核心注解)
  • Spring boot 3.3.1 官方文档 中文