策略模式:定義了算法族,分別封裝起來,讓它們之間可以互相替換,此模式讓算法的變化獨(dú)立于使用算法的客戶。
傳統(tǒng)的策略模式一般是創(chuàng)建公共接口、定義公共方法――》然后創(chuàng)建實(shí)體類實(shí)現(xiàn)公共接口、根據(jù)各自的邏輯重寫公共方法――》創(chuàng)建一個(gè)行為隨著策略對(duì)象改變而改變的 context 對(duì)象――》根據(jù)不同的傳參,調(diào)用不同的接口實(shí)現(xiàn)類方法,達(dá)到只改變參數(shù)即可獲得不同結(jié)果的目的。
但是也可以明顯發(fā)現(xiàn),這種策略模式的實(shí)現(xiàn)方式,代碼量較大,而且還要自定義要傳遞的參數(shù),可能會(huì)引入一定數(shù)量的if/else,有一定的優(yōu)化空間,接下來,我會(huì)結(jié)合實(shí)際開發(fā)經(jīng)驗(yàn),分享一種策略模式的優(yōu)化方式,進(jìn)一步優(yōu)化代碼結(jié)構(gòu)、減少代碼量。
首先,必不可少的需要?jiǎng)?chuàng)建公共接口、定義公共方法,然后創(chuàng)建實(shí)體類實(shí)現(xiàn)公共接口、根據(jù)各自的邏輯重寫公共方法,參考代碼如下:
定義公共接口CommonService,以及公共方法push()
package com.itcq.service.StrategyPattern; public interface CommonService { String push(String key); }
創(chuàng)建三個(gè)不同的接口實(shí)現(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; } }
接下來就是重點(diǎn),我們利用到springboot初始化Bean的方式結(jié)合HashMap,來實(shí)現(xiàn)對(duì)策略模式的優(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實(shí)現(xiàn)InitializingBean接口,InitializingBean接口為bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是繼承該接口的類,在初始化bean的時(shí)候都會(huì)執(zhí)行該方法。
定義一個(gè)hashmap集合,用來保存不同的公共接口實(shí)現(xiàn)類對(duì)象,這里把參數(shù)抽取成一個(gè)枚舉類,利用SpringBoot的高級(jí)容器ApplicationContext,獲取Bean對(duì)象,當(dāng)然這里直接new一個(gè)實(shí)現(xiàn)類對(duì)象也是可以的,將不同的參數(shù)和實(shí)現(xiàn)對(duì)象封裝到map集合中,實(shí)現(xiàn)參數(shù)和邏輯一一對(duì)應(yīng)。
測(cè)試方法如下,通過hashmap的key獲取對(duì)應(yīng)的實(shí)現(xiàn)類對(duì)象,這樣就不必再自定義參數(shù)類型,徹底消除了if/else,也不用暴露給方法調(diào)用者過多的業(yè)務(wù)邏輯。
public String testMethod2(String key) { CommonService commonService = hashmap.get(key); Assert.notNull(commonService, "參數(shù)錯(cuò)誤,找不到模式"); return commonService.push(key); }
最后在controller層調(diào)用方法,進(jìn)行測(cè)試:
@Autowired private TestServiceTwo testServiceTwo; @GetMapping("/test/two") public String testMethodTwo(@RequestParam(name = "key") String key) { return testServiceTwo.testMethod2(key); }
測(cè)試結(jié)果如下:
參數(shù)正確情況下:
參數(shù)錯(cuò)誤情況下:
利用這種自定義初始化bean+hashmap的方式完成了對(duì)策略模式的優(yōu)化,優(yōu)化了代碼的結(jié)構(gòu),并且徹底消除了if/else,個(gè)人認(rèn)為可以很好地提升代碼質(zhì)量。
代碼改變世界
到此這篇關(guān)于實(shí)踐講解SpringBoot自定義初始化Bean+HashMap優(yōu)化策略模式的文章就介紹到這了,更多相關(guān)SpringBoot Bean HashMap優(yōu)化策略內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.cnblogs.com/itcq/p/15293737.html