JAVA設計模式(16):行為型-策略模式(Strategy)

來源:互聯網
上載者:User

標籤:java   設計模式   策略模式   strategy   

情境
某個市場人員接到單後的報價策略(CRM系統中常見問題)報價策略很複雜,可以簡單做如下分類:
  • 普通客戶小批量報價
  • 普通客戶大批量報價
  • 老客戶小批量報價
  • 老客戶大批量報價

具體選用哪個報價策略,這需要根據實際情況來確定。這時候,我們採用策略模式即可。


我們先採用條件陳述式處理

public class Test {public double getPrice(String type,double price){if(type.equals("普通客戶小批量")){System.out.println("不打折,原價");return price;}else if(type.equals("普通客戶大批量")){System.out.println("打九折");return price*0.9;}else if(type.equals("老客戶小批量")){System.out.println("打八五折");return price*0.85;}else if(type.equals("老客戶大批量")){System.out.println("打八折");return price*0.8;}return price;}}


假如,類型特別多,演算法比較複雜時,整個條件控制碼會變的很長,難於維護。

策略模式對應於解決某一個問題的演算法族,允許使用者從該演算法族中任選一個演算法解決某一問題,
同時可以方便的更換演算法或者增加新的演算法。並且由用戶端決定調用哪個演算法。


優點

  • 可以動態改變對象的行為
缺點
  • 用戶端必須知道所有的策略類,並自行決定使用哪一個策略類
  • 策略模式將造成產生很多策略類


組成

  • 環境類(Context):用一個ConcreteStrategy對象來配置。維護一個對Strategy對象的引用。可定義一個介面來讓Strategy訪問它的資料。
  • 抽象策略類(Strategy):定義所有支援的演算法的公用介面。 Context使用這個介面來調用某ConcreteStrategy定義的演算法。
  • 具體策略類(ConcreteStrategy):以Strategy介面實現某具體演算法。

public interface DiscountStratery {public double getDiscount(double originPrice);}public class VipDicount implements DiscountStratery {public double getDiscount(double originPrice) {// 重寫getDiscount()方法,提供VIP打折演算法System.out.println("使用VIP折扣.....");return originPrice * 0.5;}}public class OldDicount implements DiscountStratery {public double getDiscount(double originPrice) {// 重寫getDiscount()方法,提供VIP打折演算法System.out.println("使用舊書折扣.....");return originPrice * 0.7;}}public class DiscountContext {// 組合一個DiscountStratery對象private DiscountStratery strategy;public DiscountContext(DiscountStratery strategy) {this.strategy = strategy;}// 根據實際所使用的DiscountStratery對象得到折扣價public double getDiscountPrice(double price) {// 如果strategy為 null 系統自動選擇OldDicount類if (strategy == null) {strategy = new OldDicount();}return this.strategy.getDiscount(price);}// 提供切換演算法的方法public void changeDiscount(DiscountStratery strategy) {this.strategy = strategy;}}public class StrategyClient {public static void main(String[] args) {// 用戶端沒有選擇打折策略類DiscountContext dc = new DiscountContext(null);double price = 79;// 使用預設的打折策略System.out.println("79元書的預設打折後的價格是:" + dc.getDiscountPrice(price));// 用戶端選擇合適的VIP打折策略dc.changeDiscount(new VipDicount());double price2 = 89;System.out.println("89元的書對VIP使用者的價格是" + dc.getDiscountPrice(price2));}}


本質
分離演算法,選擇實現。


開發中常見的情境

  1. JAVA中GUI編程中,布局管理
  2. Spring架構中,Resource介面,資源存取原則
  3. java.servlet.http.HttpServlet#service()

JAVA設計模式(16):行為型-策略模式(Strategy)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.