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

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

这里写目录标题

      • 定义接口
      • 定义抽象类
      • 定义主处理器
      • 分支处理器
      • 定义工厂
      • 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.dtcms.com/a/61526.html

相关文章:

  • 若依RuoYi-Cloud-Plus微服务版(完整版)前后端部署
  • 种子填充(Floodfill、泛滥填充、洪水填充) 算法c++模板
  • 固定表头、首列 —— uniapp、vue 项目
  • C#主流日志库深度对比:NLog、log4net与Serilog如何选择?
  • Qt 元对象系统
  • PyCharm 接入 DeepSeek、OpenAI、Gemini、Mistral等大模型完整版教程(通用)!
  • 《Mycat核心技术》第19章:基于MySQL实现读写分离
  • [数据结构]并查集--C++版本的实现代码
  • 【AI】神经网络|机器学习——图解Transformer(完整版)
  • Python数据分析之数据分析工具
  • 【C语言】--- 动态内存管理详解
  • 转自南京日报:天洑软件创新AI+仿真技术变制造为“智造
  • 网络安全反渗透 网络安全攻防渗透
  • 【性能测试】Jmeter详细操作-小白使用手册(2)
  • 常见排序算法深度评测:从原理到10万级数据实战
  • 【产品小白】Axure的简单操作
  • 【NexLM 开源系列】如何封装多个大模型 API 调用
  • QT显示网页控件QAxWidget、QWebEngineView及区别
  • Pytorch实现之利用CGAN鉴别真假图像
  • 深入解析Spring AI框架:在Java应用中实现智能化交互的关键
  • vue3+elementuiplus的table表格动态高度
  • 1-003:MySQL 的索引类型有哪些?
  • 数据结构和算法--仅仅用于理解里面的术语,入门级别
  • github生成badges的方法
  • 大模型Transformer的MOE架构介绍及方案整理
  • 记录--洛谷 P1451 求细胞数量
  • MySQL数据库的简单练习
  • 详解分辨率、像素值与图像大小:数字图像的三大支柱
  • ospf虚拟链路配置
  • 学习资料电子版 免费下载的网盘网站(非常全!)