策略模式:定義了算法族,分別封裝起來,讓它們之間可以互相替換,此模式讓算法的變化獨立於使用算法的客戶。
傳統的策略模式一般是創建公共接口、定義公共方法——》然后創建實體類實現公共接口、根據各自的邏輯重寫公共方法——》創建一個行為隨着策略對象改變而改變的 context 對象——》根據不同的傳參,調用不同的接口實現類方法,達到只改變參數即可獲得不同結果的目的。
但是也可以明顯發現,這種策略模式的實現方式,代碼量較大,而且還要自定義要傳遞的參數,可能會引入一定數量的if/else,有一定的優化空間,接下來,我會結合實際開發經驗,分享一種策略模式的優化方式,進一步優化代碼結構、減少代碼量。
首先,必不可少的需要創建公共接口、定義公共方法,然后創建實體類實現公共接口、根據各自的邏輯重寫公共方法,參考代碼如下:
定義公共接口CommonService,以及公共方法push()
1 package com.itcq.service.StrategyPattern; 2 3 public interface CommonService { 4 String push(String key); 5 }
創建三個不同的接口實現類,重寫push()方法
1 package com.itcq.service.StrategyPattern; 2 import org.springframework.stereotype.Service; 3 4 @Service 5 public class TestOne implements CommonService { 6 @Override 7 public String push(String key) { 8 return "1.這是模式:" + key; 9 } 10 }
1 package com.itcq.service.StrategyPattern; 2 3 import org.springframework.stereotype.Service; 4 5 @Service 6 public class TestTwo implements CommonService{ 7 @Override 8 public String push(String key) { 9 return "2.這是模式:"+key; 10 } 11 }
1 package com.itcq.service.StrategyPattern; 2 import org.springframework.stereotype.Service; 3 4 @Service 5 public class TestThree implements CommonService{ 6 @Override 7 public String push(String key) { 8 return "3.這是模式:"+key; 9 } 10 }
接下來就是重點,我們利用到springboot初始化Bean的方式結合HashMap,來實現對策略模式的優化
1 @Service 2 public class TestServiceTwo implements InitializingBean { 3 4 @Autowired 5 private ApplicationContext applicationContext; 6 7 private HashMap<String, CommonService> hashmap = new HashMap<>(); 8 9 @Override 10 public void afterPropertiesSet() { 11 12 hashmap.put(StrategyTestEnum.STRATEGY_ONE.getTitle(), new TestOne()); 13 hashmap.put(StrategyTestEnum.STRATEGY_TWO.getTitle(), this.applicationContext.getBean(TestTwo.class)); 14 hashmap.put(StrategyTestEnum.STRATEGY_THREE.getTitle(), this.applicationContext.getBean(TestThree.class)); 15 } 16 }
1 @Getter 2 public enum StrategyTestEnum { 3 STRATEGY_ONE("一", "模式一"), 4 STRATEGY_TWO("二", "模式二"), 5 STRATEGY_THREE("三", "模式三"), 6 ; 7 8 private String title; 9 private String value; 10 11 StrategyTestEnum(String title, String value) { 12 this.title = title; 13 this.value = value; 14 } 15 }
TestServiceTwo實現InitializingBean接口,InitializingBean接口為bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是繼承該接口的類,在初始化bean的時候都會執行該方法。
定義一個hashmap集合,用來保存不同的公共接口實現類對象,這里把參數抽取成一個枚舉類,利用SpringBoot的高級容器ApplicationContext,獲取Bean對象,當然這里直接new一個實現類對象也是可以的,將不同的參數和實現對象封裝到map集合中,實現參數和邏輯一一對應。
測試方法如下,通過hashmap的key獲取對應的實現類對象,這樣就不必再自定義參數類型,徹底消除了if/else,也不用暴露給方法調用者過多的業務邏輯。
1 public String testMethod2(String key) { 2 3 CommonService commonService = hashmap.get(key); 4 Assert.notNull(commonService, "參數錯誤,找不到模式"); 5 return commonService.push(key); 6 }
最后在controller層調用方法,進行測試:
1 @Autowired 2 private TestServiceTwo testServiceTwo; 3 4 @GetMapping("/test/two") 5 public String testMethodTwo(@RequestParam(name = "key") String key) { 6 7 return testServiceTwo.testMethod2(key); 8 }
測試結果如下:
參數正確情況下:
參數錯誤情況下:
利用這種自定義初始化bean+hashmap的方式完成了對策略模式的優化,優化了代碼的結構,並且徹底消除了if/else,個人認為可以很好地提升代碼質量。