Java设计模式(十二) 策略模式
策略模式介紹
策略模式定義
策略模式(Strategy Pattern),將各種算法封裝到具體的類中,作為一個抽象策略類的子類,使得它們可以互換??蛻舳丝梢宰孕袥Q定使用哪種算法。
策略模式類圖
策略模式類圖如下
策略模式角色劃分
- Strategy?策略接口或者(抽象策略類),定義策略執行接口
- ConcreteStrategy?具體策略類
- Context?上下文類,持有具體策略類的實例,并負責調用相關的算法
策略模式實例解析
本文代碼可從作者Github下載
典型策略模式實現
策略接口,定義策略執行接口
| 1234567 | package com.jasongj.strategy;public interface Strategy { void strategy(String input);} |
具體策略類,實現策略接口,提供具體算法
| 12345678910111213141516 | package com.jasongj.strategy;import org.slf4j.Logger;import org.slf4j.LoggerFactory;.jasongj.annotation.Strategy(name="StrategyA")public class ConcreteStrategyA implements Strategy { private static final Logger LOG = LoggerFactory.getLogger(ConcreteStrategyB.class); public void strategy(String input) {LOG.info("Strategy A for input : {}", input);}} |
| 12345678910111213141516 | package com.jasongj.strategy;import org.slf4j.Logger;import org.slf4j.LoggerFactory;.jasongj.annotation.Strategy(name="StrategyB")public class ConcreteStrategyB implements Strategy { private static final Logger LOG = LoggerFactory.getLogger(ConcreteStrategyB.class); public void strategy(String input) {LOG.info("Strategy B for input : {}", input);}} |
Context類,持有具體策略類的實例,負責調用具體算法
| 1234567891011121314151617 | package com.jasongj.context;import com.jasongj.strategy.Strategy;public class SimpleContext { private Strategy strategy; public SimpleContext(Strategy strategy) { this.strategy = strategy;} public void action(String input) {strategy.strategy(input);} } |
客戶端可以實例化具體策略類,并傳給Context類,通過Context統一調用具體算法
| 123456789101112131415 | package com.jasongj.client;import com.jasongj.context.SimpleContext;import com.jasongj.strategy.ConcreteStrategyA;import com.jasongj.strategy.Strategy;public class SimpleClient { public static void main(String[] args) {Strategy strategy = new ConcreteStrategyA();SimpleContext context = new SimpleContext(strategy);context.action("Hellow, world");}} |
使用Annotation和簡單工廠模式增強策略模式
上面的實現中,客戶端需要顯示決定具體使用何種策略,并且一旦需要換用其它策略,需要修改客戶端的代碼。解決這個問題,一個比較好的方式是使用簡單工廠,使得客戶端都不需要知道策略類的實例化過程,甚至都不需要具體哪種策略被使用。
如《Java設計模式(一) 簡單工廠模式不簡單》所述,簡單工廠的實現方式比較多,可以結合《Java系列(一)Annotation(注解)》中介紹的Annotation方法。
使用Annotation和簡單工廠模式的Context類如下
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 | package com.jasongj.context;import java.util.Collections;import java.util.Map;import java.util.Set;import java.util.concurrent.ConcurrentHashMap;import org.apache.commons.configuration.ConfigurationException;import org.apache.commons.configuration.XMLConfiguration;import org.reflections.Reflections;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.jasongj.strategy.Strategy;public class SimpleFactoryContext { private static final Logger LOG = LoggerFactory.getLogger(SimpleFactoryContext.class); private static Map<String, Class> allStrategies; static {Reflections reflections = new Reflections("com.jasongj.strategy");Set<Class<?>> annotatedClasses =reflections.getTypesAnnotatedWith(com.jasongj.annotation.Strategy.class);allStrategies = new ConcurrentHashMap<String, Class>(); for (Class<?> classObject : annotatedClasses) {com.jasongj.annotation.Strategy strategy = (com.jasongj.annotation.Strategy) classObject.getAnnotation(com.jasongj.annotation.Strategy.class);allStrategies.put(strategy.name(), classObject);}allStrategies = Collections.unmodifiableMap(allStrategies);} private Strategy strategy; public SimpleFactoryContext() {String name = null; try {XMLConfiguration config = new XMLConfiguration("strategy.xml");name = config.getString("strategy.name");LOG.info("strategy name is {}", name);} catch (ConfigurationException ex) {LOG.error("Parsing xml configuration file failed", ex);} if (allStrategies.containsKey(name)) {LOG.info("Created strategy name is {}", name); try {strategy = (Strategy) allStrategies.get(name).newInstance();} catch (InstantiationException | IllegalAccessException ex) {LOG.error("Instantiate Strategy failed", ex);}} else {LOG.error("Specified Strategy name {} does not exist", name);}} public void action(String input) {strategy.strategy(input);}} |
從上面的實現可以看出,雖然并沒有單獨創建一個簡單工廠類,但它已經融入了簡單工廠模式的設計思想和實現方法。
客戶端調用方式如下
| 123456789101112 | package com.jasongj.client;import com.jasongj.context.SimpleFactoryContext;public class SimpleFactoryClient { public static void main(String[] args) {SimpleFactoryContext context = new SimpleFactoryContext();context.action("Hellow, world");}} |
從上面代碼可以看出,引入簡單工廠模式后,客戶端不再需要直接實例化具體的策略類,也不需要判斷應該使用何種策略,可以方便應對策略的切換。
策略模式分析
策略模式優點
- 策略模式提供了對“開閉原則”的完美支持,用戶可以在不修改原有系統的基礎上選擇算法(策略),并且可以靈活地增加新的算法(策略)。
- 策略模式通過Context類提供了管理具體策略類(算法族)的辦法。
- 結合簡單工廠模式和Annotation,策略模式可以方便的在不修改客戶端代碼的前提下切換算法(策略)。
策略模式缺點
- 傳統的策略模式實現方式中,客戶端必須知道所有的具體策略類,并須自行顯示決定使用哪一個策略類。但通過本文介紹的通過和Annotation和簡單工廠模式結合,可以有效避免該問題
- 如果使用不當,策略模式可能創建很多具體策略類的實例,但可以通過使用上文《Java設計模式(十一) 享元模式》介紹的享元模式有效減少對象的數量。
策略模式已(未)遵循的OOP原則
已遵循的OOP原則
- 依賴倒置原則
- 迪米特法則
- 里氏替換原則
- 接口隔離原則
- 單一職責原則
- 開閉原則
未遵循的OOP原則
- NA
Java設計模式系列
- Java設計模式(一) 簡單工廠模式不簡單
- Java設計模式(二) 工廠方法模式
- Java設計模式(三) 抽象工廠模式
- Java設計模式(四) 觀察者模式
- Java設計模式(五) 組合模式
- Java設計模式(六) 代理模式 VS. 裝飾模式
- Java設計模式(七) Spring AOP JDK動態代理 vs. cglib
- Java設計模式(八) 適配器模式
- Java設計模式(九) 橋接模式
- Java設計模式(十) 你真的用對單例模式了嗎?
- Java設計模式(十一) 享元模式
- Java設計模式(十二) 策略模式
from:?http://www.jasongj.com/design_pattern/strategy/
總結
以上是生活随笔為你收集整理的Java设计模式(十二) 策略模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java设计模式透析之 —— 策略(St
- 下一篇: 《JAVA与模式》之策略模式