SpringBoot⾃定义初始化Bean+HashMap优化策略模式实践
策略模式:定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独⽴于使⽤算法的客户。
  传统的策略模式⼀般是创建公共接⼝、定义公共⽅法——》然后创建实体类实现公共接⼝、根据各⾃的逻辑重写公共⽅法——》创建⼀个⾏为随着策略对象改变⽽改变的context 对象——》根据不同的传参,调⽤不同的接⼝实现类⽅法,达到只改变参数即可获得不同结果的⽬的。
  但是也可以明显发现,这种策略模式的实现⽅式,代码量较⼤,⽽且还要⾃定义要传递的参数,可能会引⼊⼀定数量的if/else,有⼀定的优化空间,接下来,我会结合实际开发经验,分享⼀种策略模式的优化⽅式,进⼀步优化代码结构、减少代码量。
  ⾸先,必不可少的需要创建公共接⼝、定义公共⽅法,然后创建实体类实现公共接⼝、根据各⾃的逻辑重写公共⽅法,参考代码如下:
定义公共接⼝CommonService,以及公共⽅法push()
1package com.itcq.service.StrategyPattern;
2
3public interface CommonService {
4    String push(String key);
5 }
创建三个不同的接⼝实现类,重写push()⽅法
1package com.itcq.service.StrategyPattern;
2import org.springframework.stereotype.Service;
3
4 @Service
5public class TestOne implements CommonService {
6    @Override
7public String push(String key) {
8return "1.这是模式:" + key;
9    }
10 }
1package com.itcq.service.StrategyPattern;
2
3import org.springframework.stereotype.Service;
4
5 @Service
6public class TestTwo implements CommonService{
7    @Override
8public String push(String key) {
9return "2.这是模式:"+key;
10    }
11 }
1package com.itcq.service.StrategyPattern;
2import org.springframework.stereotype.Service;
3
4 @Service
5public class TestThree implements CommonService{
6    @Override
7public String push(String key) {
8return "3.这是模式:"+key;
9    }
10 }
接下来就是重点,我们利⽤到springboot初始化Bean的⽅式结合HashMap,来实现对策略模式的优化
1 @Service
2public class TestServiceTwo implements InitializingBean {
3
4    @Autowired
5private ApplicationContext applicationContext;
6
7private HashMap<String, CommonService> hashmap = new HashMap<>();
8
9    @Override
10public void afterPropertiesSet() {
11
12        hashmap.put(StrategyTestEnum.Title(), new TestOne());
13        hashmap.put(StrategyTestEnum.Title(), Bean(TestTwo.class));
14        hashmap.put(StrategyTestEnum.Title(), Bean(TestThree.class));
15    }
16 }
1 @Getter
2public enum StrategyTestEnum {
3    STRATEGY_ONE("⼀", "模式⼀"),
4    STRATEGY_TWO("⼆", "模式⼆"),
5    STRATEGY_THREE("三", "模式三"),
6    ;
7
8private String title;
9private String value;
10
11    StrategyTestEnum(String title, String value) {
12this.title = title;
13this.value = value;
14    }
15 }
TestServiceTwo实现InitializingBean接⼝,InitializingBean接⼝为bean提供了初始化⽅法的⽅式,它只包括afterPropertiesSet⽅法,凡是继承该接⼝的类,在初始化bean的时候都会执⾏该⽅法。
定义⼀个hashmap集合,⽤来保存不同的公共接⼝实现类对象,这⾥把参数抽取成⼀个枚举类,利⽤SpringBoot的⾼级容器ApplicationContext,获取Bean对象,当然这⾥直接new⼀个实现类对象也是可以的,将不同的参数和实现对象封装到map集合中,实现参数和逻辑⼀⼀对应。
测试⽅法如下,通过hashmap的key获取对应的实现类对象,这样就不必再⾃定义参数类型,彻底消除了if/else,也不⽤暴露给⽅法调⽤者过多的业务逻辑。
1public String testMethod2(String key) {
2
3        CommonService commonService = (key);
4        Null(commonService, "参数错误,不到模式");
5return commonService.push(key);
6    }
最后在controller层调⽤⽅法,进⾏测试:
1    @Autowired
2private TestServiceTwo testServiceTwo;spring framework开发参考手册
3
4    @GetMapping("/test/two")
5public String testMethodTwo(@RequestParam(name = "key") String key) {
6
stMethod2(key);
8    }
测试结果如下:
参数正确情况下:
参数错误情况下:
   利⽤这种⾃定义初始化bean+hashmap的⽅式完成了对策略模式的优化,优化了代码的结构,并且彻底消除了if/else,个⼈认为可以很好地提升代码质量。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。