设计模式18---设计模式之策略模式(Strategy)(行为型)
1.場景模擬
簡單的報價管理系統:
對于普通用戶和新用戶報全價
對于老客戶統一折扣5%
對于大客戶統一折扣10%
2.不用模式的解決方案
package demo16.strategy.example2; /*** 價格管理,主要完成計算向客戶所報價格的功能*/ public class Price {/*** 報價,對不同類型的,計算不同的價格* @param goodsPrice 商品銷售原價* @param customerType 客戶類型* @return 計算出來的,應該給客戶報的價格*/public double quote(double goodsPrice,String customerType){if("普通客戶".equals(customerType)){return this.calcPriceForNormal(goodsPrice);}else if("老客戶".equals(customerType)){return this.calcPriceForOld(goodsPrice);}else if("大客戶".equals(customerType)){return this.calcPriceForLarge(goodsPrice); }//其余人員都是報原價return goodsPrice;}/*** 為新客戶或者是普通客戶計算應報的價格* @param goodsPrice 商品銷售原價* @return 計算出來的,應該給客戶報的價格*/private double calcPriceForNormal(double goodsPrice){System.out.println("對于新客戶或者是普通客戶,沒有折扣");return goodsPrice;}/*** 為老客戶計算應報的價格* @param goodsPrice 商品銷售原價* @return 計算出來的,應該給客戶報的價格*/private double calcPriceForOld(double goodsPrice){System.out.println("對于老客戶,統一折扣5%");return goodsPrice*(1-0.05);}/*** 為大客戶計算應報的價格* @param goodsPrice 商品銷售原價* @return 計算出來的,應該給客戶報的價格*/private double calcPriceForLarge(double goodsPrice){System.out.println("對于大客戶,統一折扣10%");return goodsPrice*(1-0.1); } }3.有何問題?
會經常有這樣的需要,在公司周年慶的時候,所有的客戶額外增加3%的折扣,在換季的時候普通用戶額外增加3%的折扣,然后過了促銷時間,價格又要漲回來,那么這個價格類將會非常的龐大,而且,方法非常的多。
看到這,朋友們很快就想到:如何實現才能讓價格類中的計算報價的算法,很容易的實現可維護,可擴展,而且可以動態的切換變化呢?
4.使用策略模式來解決問題
4.1策略模式定義
定義一系列的算法,把他們一個個封裝起來,并且可以使他們相互替換,本模式使得算法可獨立于使用它的客戶而變化。
4.2策略模式的結構圖
?
4.3策略模式示例代碼
package demo16.strategy.example3;/*** 策略,定義算法的接口*/ public interface Strategy {/*** 某個算法的接口,可以有傳入參數,也可以有返回值*/public void algorithmInterface(); } ********************************************************************* package demo16.strategy.example3;/*** 實現具體的算法*/ public class ConcreteStrategyA implements Strategy {public void algorithmInterface() {//具體的算法實現 }} ********************************************************************* package demo16.strategy.example3;/*** 實現具體的算法*/ public class ConcreteStrategyB implements Strategy {public void algorithmInterface() {//具體的算法實現 }}package demo16.strategy.example3;/*** 實現具體的算法*/ public class ConcreteStrategyC implements Strategy {public void algorithmInterface() {//具體的算法實現 } } ********************************************************************* package demo16.strategy.example3;/*** 上下文對象,通常會持有一個具體的策略對象*/ public class Context {/*** 持有一個具體的策略對象*/private Strategy strategy;/*** 構造方法,傳入一個具體的策略對象* @param aStrategy 具體的策略對象*/public Context(Strategy aStrategy) {this.strategy = aStrategy;}/*** 上下文對客戶端提供的操作接口,可以有參數和返回值*/public void contextInterface() {//通常會轉調具體的策略對象進行算法運算strategy.algorithmInterface();} }5.使用策略模式重寫實例
package demo16.strategy.example4;/*** 策略,定義計算報價算法的接口*/ public interface Strategy {/*** 計算應報的價格* @param goodsPrice 商品銷售原價* @return 計算出來的,應該給客戶報的價格*/public double calcPrice(double goodsPrice); } ************************************************************************ package demo16.strategy.example4; /*** 具體算法實現,為新客戶或者是普通客戶計算應報的價格*/ public class NormalCustomerStrategy implements Strategy{public double calcPrice(double goodsPrice) {System.out.println("對于新客戶或者是普通客戶,沒有折扣");return goodsPrice;} } *********************************************************************** package demo16.strategy.example4; /*** 具體算法實現,為老客戶計算應報的價格*/ public class OldCustomerStrategy implements Strategy{public double calcPrice(double goodsPrice) {System.out.println("對于老客戶,統一折扣5%");return goodsPrice*(1-0.05);} } *********************************************************************** package demo16.strategy.example4; /*** 具體算法實現,為大客戶計算應報的價格*/ public class LargeCustomerStrategy implements Strategy{public double calcPrice(double goodsPrice) {System.out.println("對于大客戶,統一折扣10%");return goodsPrice*(1-0.1);} } *********************************************************************** package demo16.strategy.example4; /*** 價格管理,主要完成計算向客戶所報價格的功能*/ public class Price {/*** 持有一個具體的策略對象*/private Strategy strategy = null;/*** 構造方法,傳入一個具體的策略對象* @param aStrategy 具體的策略對象*/public Price(Strategy aStrategy){this.strategy = aStrategy;} /*** 報價,計算對客戶的報價* @param goodsPrice 商品銷售原價* @return 計算出來的,應該給客戶報的價格*/public double quote(double goodsPrice){return this.strategy.calcPrice(goodsPrice);} } *********************************************************************** package demo16.strategy.example4;public class Client {public static void main(String[] args) {//1:選擇并創建需要使用的策略對象Strategy strategy = new LargeCustomerStrategy();//2:創建上下文Price ctx = new Price(strategy);//3:計算報價double quote = ctx.quote(1000);System.out.println("向客戶報價:"+quote);} }6.模式講解
6.1要點
功能:把具體的算法從具體的業務處理中獨立出來,把他們實現成為單獨的算法類,從而形成一系列的算法,并讓這些算法可以相互替換。
策略算法是相同行為的不同實現
什么時候選用:多個if-else語句的時候就可以選用
6.2策略模式的調用順序示意圖
首先客戶端選擇并創建具體的策略對象
其次創建上下文
最后調用上下文的方法來執行功能了
?
6.3當添加新的策略時候
只需要動下面代碼,是不是很簡單,很方便呢?
//1:選擇并創建需要使用的策略對象
Strategy strategy = new LargeCustomerStrategy();
6.4另一種策略模式調用示意圖
?
6.5策略模式優缺點
優點:定義一系列算法,避免使用多重條件語句,更好的擴展性
缺點:客戶端必須了解每種策略的不同,增加了對象數目,只適合扁平的算法結構(地位平等的算法)
6.6設計模式的本質
分離算法,選擇實現
總結
以上是生活随笔為你收集整理的设计模式18---设计模式之策略模式(Strategy)(行为型)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EJB教程实例
- 下一篇: HDU 4267 A Simple Pr