實踐講解SpringBoot自定義初始化Bean+HashMap優(yōu)化策略模式
策略模式:定義了算法族,分別封裝起來,讓它們之間可以互相替換,此模式讓算法的變化獨立于使用算法的客戶。
傳統(tǒng)的策略模式一般是創(chuàng)建公共接口、定義公共方法——》然后創(chuàng)建實體類實現(xiàn)公共接口、根據(jù)各自的邏輯重寫公共方法——》創(chuàng)建一個行為隨著策略對象改變而改變的 context 對象——》根據(jù)不同的傳參,調(diào)用不同的接口實現(xiàn)類方法,達到只改變參數(shù)即可獲得不同結(jié)果的目的。


但是也可以明顯發(fā)現(xiàn),這種策略模式的實現(xiàn)方式,代碼量較大,而且還要自定義要傳遞的參數(shù),可能會引入一定數(shù)量的if/else,有一定的優(yōu)化空間,接下來,我會結(jié)合實際開發(fā)經(jīng)驗,分享一種策略模式的優(yōu)化方式,進一步優(yōu)化代碼結(jié)構(gòu)、減少代碼量。
首先,必不可少的需要創(chuàng)建公共接口、定義公共方法,然后創(chuàng)建實體類實現(xiàn)公共接口、根據(jù)各自的邏輯重寫公共方法,參考代碼如下:
定義公共接口CommonService,以及公共方法push()
package com.itcq.service.StrategyPattern;
public interface CommonService {
String push(String key);
}
創(chuàng)建三個不同的接口實現(xiàn)類,重寫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的方式結(jié)合HashMap,來實現(xiàn)對策略模式的優(yōu)化
@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實現(xiàn)InitializingBean接口,InitializingBean接口為bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是繼承該接口的類,在初始化bean的時候都會執(zhí)行該方法。
定義一個hashmap集合,用來保存不同的公共接口實現(xiàn)類對象,這里把參數(shù)抽取成一個枚舉類,利用SpringBoot的高級容器ApplicationContext,獲取Bean對象,當(dāng)然這里直接new一個實現(xiàn)類對象也是可以的,將不同的參數(shù)和實現(xiàn)對象封裝到map集合中,實現(xiàn)參數(shù)和邏輯一一對應(yīng)。
測試方法如下,通過hashmap的key獲取對應(yīng)的實現(xiàn)類對象,這樣就不必再自定義參數(shù)類型,徹底消除了if/else,也不用暴露給方法調(diào)用者過多的業(yè)務(wù)邏輯。
public String testMethod2(String key) {
CommonService commonService = hashmap.get(key);
Assert.notNull(commonService, "參數(shù)錯誤,找不到模式");
return commonService.push(key);
}
最后在controller層調(diào)用方法,進行測試:
@Autowired
private TestServiceTwo testServiceTwo;
@GetMapping("/test/two")
public String testMethodTwo(@RequestParam(name = "key") String key) {
return testServiceTwo.testMethod2(key);
}
測試結(jié)果如下:
參數(shù)正確情況下:

參數(shù)錯誤情況下:


利用這種自定義初始化bean+hashmap的方式完成了對策略模式的優(yōu)化,優(yōu)化了代碼的結(jié)構(gòu),并且徹底消除了if/else,個人認(rèn)為可以很好地提升代碼質(zhì)量。
代碼改變世界
到此這篇關(guān)于實踐講解SpringBoot自定義初始化Bean+HashMap優(yōu)化策略模式的文章就介紹到這了,更多相關(guān)SpringBoot Bean HashMap優(yōu)化策略內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Spring Cloud Config結(jié)合Bus實現(xiàn)分布式配置中心的步驟
這篇文章主要介紹了利用Spring Cloud Config結(jié)合Bus實現(xiàn)分布式配置中心的相關(guān)資料,文中通過示例代碼將實現(xiàn)的步驟一步步介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友下面來一起看看吧2018-05-05
解決?IDEA?Maven?項目中"Could?not?find?artifact"?
這篇文章主要介紹了解決IDEA Maven項目中Could not?find?artifact問題的常見情況和解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07

