SpringBoot自定义初始化Bean+HashMap优化策略模式实践
策略模式:定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
传统的策略模式一般是创建公共接口、定义公共方法——》然后创建实体类实现公共接口、根据各自的逻辑重写公共方法——》创建一个行为随着策略对象改变而改变的 context 对象——》根据不同的传参,调用不同的接口实现类方法,达到只改变参数即可获得不同结果的目的。
但是也可以明显发现,这种策略模式的实现方式,代码量较大,而且还要自定义要传递的参数,可能会引入一定数量的if/else,有一定的优化空间,接下来,我会结合实际开发经验,分享一种策略模式的优化方式,进一步优化代码结构、减少代码量。
首先,必不可少的需要创建公共接口、定义公共方法,然后创建实体类实现公共接口、根据各自的逻辑重写公共方法,参考代码如下:
定义公共接口CommonService,以及公共方法push()
1 package com.itcq.service.StrategyPattern; 2 3 public interface CommonService { 4 String push(String key); 5 }
创建三个不同的接口实现类,重写push()方法
1 package com.itcq.service.StrategyPattern; 2 import org.springframework.stereotype.Service; 3 4 @Service 5 public class TestOne implements CommonService { 6 @Override 7 public String push(String key) { 8 return "1.这是模式:" + key; 9 } 10 }
1 package com.itcq.service.StrategyPattern; 2 3 import org.springframework.stereotype.Service; 4 5 @Service 6 public class TestTwo implements CommonService{ 7 @Override 8 public String push(String key) { 9 return "2.这是模式:"+key; 10 } 11 }
1 package com.itcq.service.StrategyPattern; 2 import org.springframework.stereotype.Service; 3 4 @Service 5 public class TestThree implements CommonService{ 6 @Override 7 public String push(String key) { 8 return "3.这是模式:"+key; 9 } 10 }
接下来就是重点,我们利用到springboot初始化Bean的方式结合HashMap,来实现对策略模式的优化
1 @Service 2 public class TestServiceTwo implements InitializingBean { 3 4 @Autowired 5 private ApplicationContext applicationContext; 6 7 private HashMap<String, CommonService> hashmap = new HashMap<>(); 8 9 @Override 10 public void afterPropertiesSet() { 11 12 hashmap.put(StrategyTestEnum.STRATEGY_ONE.getTitle(), new TestOne()); 13 hashmap.put(StrategyTestEnum.STRATEGY_TWO.getTitle(), this.applicationContext.getBean(TestTwo.class)); 14 hashmap.put(StrategyTestEnum.STRATEGY_THREE.getTitle(), this.applicationContext.getBean(TestThree.class)); 15 } 16 }
1 @Getter 2 public enum StrategyTestEnum { 3 STRATEGY_ONE("一", "模式一"), 4 STRATEGY_TWO("二", "模式二"), 5 STRATEGY_THREE("三", "模式三"), 6 ; 7 8 private String title; 9 private String value; 10 11 StrategyTestEnum(String title, String value) { 12 this.title = title; 13 this.value = value; 14 } 15 }
TestServiceTwo实现InitializingBean接口,InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。
定义一个hashmap集合,用来保存不同的公共接口实现类对象,这里把参数抽取成一个枚举类,利用SpringBoot的高级容器ApplicationContext,获取Bean对象,当然这里直接new一个实现类对象也是可以的,将不同的参数和实现对象封装到map集合中,实现参数和逻辑一一对应。
测试方法如下,通过hashmap的key获取对应的实现类对象,这样就不必再自定义参数类型,彻底消除了if/else,也不用暴露给方法调用者过多的业务逻辑。
1 public String testMethod2(String key) { 2 3 CommonService commonService = hashmap.get(key); 4 Assert.notNull(commonService, "参数错误,找不到模式"); 5 return commonService.push(key); 6 }
最后在controller层调用方法,进行测试:
1 @Autowired 2 private TestServiceTwo testServiceTwo; 3 4 @GetMapping("/test/two") 5 public String testMethodTwo(@RequestParam(name = "key") String key) { 6 7 return testServiceTwo.testMethod2(key); 8 }
测试结果如下:
参数正确情况下:
参数错误情况下:
利用这种自定义初始化bean+hashmap的方式完成了对策略模式的优化,优化了代码的结构,并且彻底消除了if/else,个人认为可以很好地提升代码质量。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步