Java與設計模式-策略模式

來源:互聯網
上載者:User

Java與設計模式-策略模式

在實際開發中,可能會遇到這樣一個情況,某一功能的實現分為多種演算法,這些演算法可以認定為策略,在實際操作時選擇不同演算法或策略進行操作得出最終結果。在實際生活中,這些例子也是舉不勝舉,例如,商場舉行活動,滿100元減10元,滿200元減30元,滿500元減100元等等...這樣每消費一筆,根據這一筆錢消費的多少,計算最終應支付的錢對應著不同的演算法,這些對應的不同計算方法就可以認定為是不同的策略。在某東購物時,根據不同的使用者等級,打折力度也是不同的。

策略模式的UML類圖參照:

假如沒有策略模式,實現某東購物計算最終付款的方法怎樣呢?

package com.strategy.demo;public class NoStrategy {/** * 不使用測量模式實現 * @param args */private static final int NOCARDUSER=1;private static final int IRONCARDUSER=2;private static final int GOLDCARDUSER=3;public static void main(String[] args) {NoStrategy NoStrategy1=new NoStrategy();NoStrategy NoStrategy2=new NoStrategy();NoStrategy NoStrategy3=new NoStrategy();System.out.println("沒有卡牌的買家買100元貨物最終應付款:"+NoStrategy1.getPrice(100.0, 1));System.out.println("鐵牌的買家買100元貨物最終應付款:"+NoStrategy2.getPrice(100.0, 2));System.out.println("金牌的買家買100元貨物最終應付款:"+NoStrategy3.getPrice(100.0, 3));}private double getNoCardPrice(double price){ return price;}private double getIronCardPrice(double price){ return price*0.9;}private double getGoldCardPrice(double price){ return price*0.7;}private double getPrice(double price,int type){ if(type==NOCARDUSER){ return getNoCardPrice(price); }else if(type ==IRONCARDUSER){ return getIronCardPrice(price); }else if(type ==GOLDCARDUSER){ return getGoldCardPrice(price); }else { return 0; }}}

運行執行個體:

呀,得出正確的答案了,這時你是不是應該滿足了呢,應該高枕無憂了呢?突然,主管說要增加鑽石使用者的類別,鑽石使用者打六折,這時你怎麼實現呢?在裡面在增加鑽石使用者的類型,再增加計算鑽石使用者的方法,再再最後的判斷裡增加i f else? 這樣的確可以實現功能,但是是不是不滿足開閉原則呢?而且隨著使用者種類的不斷增加,你的if else是不是也越來越長,邏輯也越來越複雜呢?導致系統擴充性和穩定性越來越差呢? 所以,這種方式在實際中顯然實不可取的,下面我們看一下如何使用原則模式來實現上面的需求。

1.PriceStrategyInterface 介面,對應UML類圖中的Strategy介面:

package com.strategy.demo;public interface PriceStrategyInterface {double calPrice(double price);}


2.實作類別,無卡使用者:

package com.strategy.demo;public class NoCardUserStrategy implements PriceStrategyInterface {/** * 無牌買家,原價 */@Overridepublic double calPrice(double price) {return price;}}


3.實作類別,鐵卡使用者:

package com.strategy.demo;public class IronCardUserStrategy implements PriceStrategyInterface {/* * 鐵牌買家 * (non-Javadoc) * @see com.strategy.demo.PriceStrategyInterface#calPrice(double) */@Overridepublic double calPrice(double price) {return price*0.9;}}


4.實作類別,金卡使用者:

package com.strategy.demo;public class GoldCardUserStrategy implements PriceStrategyInterface {/** * 金牌買家 */@Overridepublic double calPrice(double price) {return price*0.7;}}


5.環境對象,用來操作策略:

package com.strategy.demo;public class PriceContext {/** * 操作類 */PriceStrategyInterface priceStrategyInterface;/* * 通過初始化傳入對象 */public PriceContext(PriceStrategyInterface priceStrategyInterface) {this.priceStrategyInterface=priceStrategyInterface;}/* * 通過對象計算傳回值 */public double getPrice(double price){ return priceStrategyInterface.calPrice(price);}}

環境對象初始化時,將對應的策略對象傳入,然後調用方法返回計算值。

6.構建測試類別,測試:

package com.strategy.demo;public class TestClass {public static void main(String[] args) {PriceContext priceContext=new PriceContext(new NoCardUserStrategy());System.out.println("沒有卡牌的買家買100元貨物最終應付款:"+priceContext.getPrice(100.0));PriceContext priceContext2=new PriceContext(new IronCardUserStrategy());System.out.println("鐵牌的買家買100元貨物最終應付款:"+priceContext2.getPrice(100.0));PriceContext priceContext3=new PriceContext(new GoldCardUserStrategy());System.out.println("金牌的買家買100元貨物最終應付款:"+priceContext3.getPrice(100.0));}}

運行上面的執行個體:

得到了和第一個方法一樣的正確答案,這時我們假如要增加一個鑽石買家的種類,怎麼實現呢?我們只需要增加一個策略實作類別:

package com.strategy.demo;public class DiamondUserStrategy implements PriceStrategyInterface {@Overridepublic double calPrice(double price) {return price*0.6;}}


然後測試類別增加一條鑽石類買家的購物:

package com.strategy.demo;public class TestClass {public static void main(String[] args) {PriceContext priceContext=new PriceContext(new NoCardUserStrategy());System.out.println("沒有卡牌的買家買100元貨物最終應付款:"+priceContext.getPrice(100.0));PriceContext priceContext2=new PriceContext(new IronCardUserStrategy());System.out.println("鐵牌的買家買100元貨物最終應付款:"+priceContext2.getPrice(100.0));PriceContext priceContext3=new PriceContext(new GoldCardUserStrategy());System.out.println("金牌的買家買100元貨物最終應付款:"+priceContext3.getPrice(100.0));PriceContext priceContext4=new PriceContext(new DiamondUserStrategy());System.out.println("鑽石卡的買家買100元貨物最終應付款:"+priceContext4.getPrice(100.0));}}

運行執行個體:



是不是擴充起來特別容易?條理也十分清晰。總結一下策略模式的優點:

1. 結構清晰,使用簡單直觀;

2. 系統耦合性降低,擴充方便;

3. 操作封裝徹底,資料更為安全。(在TestClass中,只知道相關實作類別,並不涉及具體計算方法)

當然,策略方式也存在一定的缺點:

由圖可以直觀的看出,隨著策略的不斷增加,子類數量變得龐大。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.