spring|SpringBoot自定义初始化Bean+HashMap优化策略模式实践

策略模式:定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
传统的策略模式一般是创建公共接口、定义公共方法——》然后创建实体类实现公共接口、根据各自的逻辑重写公共方法——》创建一个行为随着策略对象改变而改变的 context 对象——》根据不同的传参,调用不同的接口实现类方法,达到只改变参数即可获得不同结果的目的。
spring|SpringBoot自定义初始化Bean+HashMap优化策略模式实践
文章图片

spring|SpringBoot自定义初始化Bean+HashMap优化策略模式实践
文章图片

但是也可以明显发现,**这种策略模式的实现方式,代码量较大,而且还要自定义要传递的参数,可能会引入一定数量的if/else,**有一定的优化空间,接下来,我会结合实际开发经验,分享一种策略模式的优化方式,进一步优化代码结构、减少代码量。
首先,必不可少的需要创建公共接口、定义公共方法,然后创建实体类实现公共接口、根据各自的逻辑重写公共方法,参考代码如下:
定义公共接口CommonService,以及公共方法push()

1 package com.itcq.service.StrategyPattern; 2 3 public interface CommonService {4String push(String key); 5 }

【spring|SpringBoot自定义初始化Bean+HashMap优化策略模式实践】创建三个不同的接口实现类,重写push()方法
package com.itcq.service.StrategyPattern; import org.springframework.stereotype.Service; @Service public class TestOne implements CommonService {@Override public String push(String key) {return "1.这是模式:" + key; } }

package com.itcq.service.StrategyPattern; import org.springframework.stereotype.Service; @Service public class TestTwo implements CommonService{@Override public String push(String key) {return "2.这是模式:"+key; } }

package com.itcq.service.StrategyPattern; import org.springframework.stereotype.Service; @Service public class TestThree implements CommonService{@Override public String push(String key) {return "3.这是模式:"+key; } }

接下来就是重点,我们利用到springboot初始化Bean的方式结合HashMap,来实现对策略模式的优化
@Service public class TestServiceTwo implements InitializingBean {@Autowired private ApplicationContext applicationContext; private HashMap, CommonService> hashmap = new HashMap<>(); @Override public void afterPropertiesSet() {hashmap.put(StrategyTestEnum.STRATEGY_ONE.getTitle(), new TestOne()); hashmap.put(StrategyTestEnum.STRATEGY_TWO.getTitle(), this.applicationContext.getBean(TestTwo.class)); hashmap.put(StrategyTestEnum.STRATEGY_THREE.getTitle(), this.applicationContext.getBean(TestThree.class)); } }

@Getter public enum StrategyTestEnum {STRATEGY_ONE("一", "模式一"), STRATEGY_TWO("二", "模式二"), STRATEGY_THREE("三", "模式三"), ; private String title; private String value; StrategyTestEnum(String title, String value) {this.title = title; this.value = https://www.it610.com/article/value; } }

TestServiceTwo实现InitializingBean接口,InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。
定义一个hashmap集合,用来保存不同的公共接口实现类对象,这里把参数抽取成一个枚举类,利用SpringBoot的高级容器ApplicationContext,获取Bean对象,当然这里直接new一个实现类对象也是可以的,将不同的参数和实现对象封装到map集合中,实现参数和逻辑一一对应。
测试方法如下,通过hashmap的key获取对应的实现类对象,这样就不必再自定义参数类型,彻底消除了if/else,也不用暴露给方法调用者过多的业务逻辑。
1 public String testMethod2(String key) {2 3CommonService commonService = hashmap.get(key); 4Assert.notNull(commonService, "参数错误,找不到模式"); 5return commonService.push(key); 6}

最后在controller层调用方法,进行测试:
@Autowired private TestServiceTwo testServiceTwo; @GetMapping("/test/two") public String testMethodTwo(@RequestParam(name = "key") String key) {return testServiceTwo.testMethod2(key); }

测试结果如下:
参数正确情况下:
spring|SpringBoot自定义初始化Bean+HashMap优化策略模式实践
文章图片

参数错误情况下:
spring|SpringBoot自定义初始化Bean+HashMap优化策略模式实践
文章图片

spring|SpringBoot自定义初始化Bean+HashMap优化策略模式实践
文章图片

利用这种自定义初始化bean+hashmap的方式完成了对策略模式的优化,优化了代码的结构,并且彻底消除了if/else,个人认为可以很好地提升代码质量。

    推荐阅读