服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 实践讲解SpringBoot自定义初始化Bean+HashMap优化策略模式

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

2022-01-07 13:25Simpleeee Java教程

本篇讲解了SpringBoot自定义初始化Bean+HashMap优化策略模式,通过实践的方式更通俗易懂,对此不了解的同学跟着小编往下看吧

策略模式:定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。

传统的策略模式一般是创建公共接口、定义公共方法――》然后创建实体类实现公共接口、根据各自的逻辑重写公共方法――》创建一个行为随着策略对象改变而改变的 context 对象――》根据不同的传参,调用不同的接口实现类方法,达到只改变参数即可获得不同结果的目的。

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

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

但是也可以明显发现,这种策略模式的实现方式,代码量较大,而且还要自定义要传递的参数,可能会引入一定数量的if/else,有一定的优化空间,接下来,我会结合实际开发经验,分享一种策略模式的优化方式,进一步优化代码结构、减少代码量。

首先,必不可少的需要创建公共接口、定义公共方法,然后创建实体类实现公共接口、根据各自的逻辑重写公共方法,参考代码如下:

定义公共接口CommonService,以及公共方法push()

package com.itcq.service.StrategyPattern;
 
public interface CommonService {
    String push(String key);
}

创建三个不同的接口实现类,重写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<String, 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 = value;
    }
}

TestServiceTwo实现InitializingBean接口,InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。

定义一个hashmap集合,用来保存不同的公共接口实现类对象,这里把参数抽取成一个枚举类,利用SpringBoot的高级容器ApplicationContext,获取Bean对象,当然这里直接new一个实现类对象也是可以的,将不同的参数和实现对象封装到map集合中,实现参数和逻辑一一对应。

测试方法如下,通过hashmap的key获取对应的实现类对象,这样就不必再自定义参数类型,彻底消除了if/else,也不用暴露给方法调用者过多的业务逻辑。

public String testMethod2(String key) {

        CommonService commonService = hashmap.get(key);
        Assert.notNull(commonService, "参数错误,找不到模式");
        return commonService.push(key);
    }

最后在controller层调用方法,进行测试:

@Autowired
    private TestServiceTwo testServiceTwo;

    @GetMapping("/test/two")
    public String testMethodTwo(@RequestParam(name = "key") String key) {

        return testServiceTwo.testMethod2(key);
    }

测试结果如下:

参数正确情况下:

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

参数错误情况下:

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

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

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

代码改变世界

到此这篇关于实践讲解SpringBoot自定义初始化Bean+HashMap优化策略模式的文章就介绍到这了,更多相关SpringBoot Bean HashMap优化策略内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/itcq/p/15293737.html

延伸 · 阅读

精彩推荐