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

策略模式、工厂模式和模板模式的应用

1、策略模式、工厂模式解决if else

Cal

package com.example.dyc.cal;import org.springframework.beans.factory.InitializingBean;public interface Cal extends InitializingBean {public Integer cal(Integer a, Integer b);
}

Cal工厂

package com.example.dyc.cal;import org.springframework.util.StringUtils;import java.util.HashMap;public class CalFactory {public static HashMap<String, Cal> calMap = new HashMap<>();public static Cal getCal(String name){return calMap.get(name);}public static void register(String name, Cal cal){if(StringUtils.isEmpty(name)||null == cal){return;}calMap.put(name,cal);}
}

Cal具体实现Add

package com.example.dyc.cal;import org.springframework.stereotype.Component;@Component
public class Add implements Cal {@Overridepublic Integer cal(Integer a, Integer b) {return a + b;}//    public Add(){
//        CalFactory.register("add", this);
//    }@Overridepublic void afterPropertiesSet() throws Exception {CalFactory.register("add", this);}
}

Cal具体实现Sub

package com.example.dyc.cal;import org.springframework.stereotype.Component;@Component
public class Sub implements Cal{@Overridepublic Integer cal(Integer a, Integer b) {return a - b;}//    public Sub(){
//        CalFactory.register("sub", this);
//    }@Overridepublic void afterPropertiesSet() throws Exception {CalFactory.register("sub", this);}
}

测试类

package com.example.dyc.cal;import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class CalFactoryTest {@Testvoid MyTest(){Cal add = CalFactory.getCal("add");Integer cal = add.cal(1, 8);System.out.println(cal);}
}

测试类具体使用参考以往操作

2、策略模式、工厂模式,模板模式实现代码复用

 Cal

package com.example.dyc.cal;import org.springframework.beans.factory.InitializingBean;public abstract class Cal implements InitializingBean {protected abstract Integer cal(Integer a, Integer b);public Integer secretCal(Integer a, Integer b){a = secretCode(a);b = secretCode(b);Integer cal = cal(a, b);return cal;}private Integer secretCode(Integer a){return a * a;}//    Integer unsupport(){
//        throw new UnsupportedOperationException();
//    }
}

Cal工厂

package com.example.dyc.cal;import org.springframework.util.StringUtils;import java.util.HashMap;public class CalFactory {public static HashMap<String, Cal> calMap = new HashMap<>();public static Cal getCal(String name){return calMap.get(name);}public static void register(String name, Cal cal){if(StringUtils.isEmpty(name)||null == cal){return;}calMap.put(name,cal);}
}

Cal具体实现Add

package com.example.dyc.cal;import org.springframework.stereotype.Component;@Component
public class Add extends Cal {@Overrideprotected Integer cal(Integer a, Integer b) {return a + b;}//    public Add(){
//        CalFactory.register("add", this);
//    }@Overridepublic void afterPropertiesSet() throws Exception {CalFactory.register("add", this);}
}

Cal具体实现Sub

package com.example.dyc.cal;import org.springframework.stereotype.Component;@Component
public class Sub extends Cal{@Overrideprotected Integer cal(Integer a, Integer b) {return a - b;}//    public Sub(){
//        CalFactory.register("sub", this);
//    }@Overridepublic void afterPropertiesSet() throws Exception {CalFactory.register("sub", this);}
}

测试类

package com.example.dyc.cal;import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class CalFactoryTest {@Testvoid MyTest(){Cal add = CalFactory.getCal("add");Integer cal = add.secretCal(1, 8);System.out.println(cal);}
}

测试类具体使用参考以往操作

http://www.lryc.cn/news/388781.html

相关文章:

  • 在postman中调试supabase的API接口
  • 微信小程序毕业设计-英语互助系统项目开发实战(附源码+论文)
  • 【WEB前端2024】3D智体编程:乔布斯3D纪念馆-第49课-机器人自动跳舞
  • 【LLM教程-llama】如何Fine Tuning大语言模型?
  • PHP 比 Java 的开发效率高在哪?
  • 高德定位获取详细位置失败的处理方法
  • PX2平台Pytorch源码编译
  • 昇思25天学习打卡营第6天|简单的深度学习模型实战 - 函数式自动微分
  • 基于Linux的云端垃圾分类助手
  • 【PYG】Planetoid中边存储的格式,为什么打印前十条边用edge_index[:, :10]
  • 【知识图谱系列】(实例)python操作neo4j构建企业间的业务往来的知识图谱
  • 解决MySQL删除/var/lib/mysql下的所有文件后无法启动的问题
  • 探索WebKit的Flexbox奇境:CSS Flexbox支持全解析
  • Unity--协程--Coroutine
  • 详解COB显示屏的技术特点
  • 富唯智能推出的AMR复合机器人铝板CNC上下料方案
  • springcloud-config服务器,同样的配置在linux环境下不生效
  • 写代码,为什么还需要作图?
  • 一句话介绍什么是AI智能体?
  • 32.哀家要长脑子了!
  • Vue2 - 项目上线后生产环境中去除console.log的输出以及断点的解决方案
  • phpword生成PDF
  • Linux进程优先级
  • 每日一题——Python实现PAT乙级1096 大美数(举一反三+思想解读+逐步优化)3千字好文
  • 无锁编程——从CPU缓存一致性讲到内存模型(1)
  • C++编程(七)继承
  • 【ACM_2023】3D Gaussian Splatting for Real-Time Radiance Field Rendering
  • 【TB作品】atmega16 计算器,ATMEGA16单片机,Proteus仿真
  • C++的IO流操作
  • MacOS升级指定Python版本的pip