設計模式培訓之四:策略模式

來源:互聯網
上載者:User

查看本人文章索引請通過http://www.cnblogs.com/seesea125/archive/2012/04/17/2453256.html

一、定義

策略模式定義了一系列的演算法,並將每一個演算法封裝起來,而且使它們還可以相互替換。策略模式讓演算法獨立於使用它的客戶而獨立變化。

二、概述

應用情境:

  1、 多個類只區別在表現行為不同,可以使用Strategy模式,在運行時動態選擇具體要執行的行為。

  2、 需要在不同情況下使用不同的策略(演算法),或者策略還可能在未來用其它方式來實現。

  3、 對客戶隱藏具體策略(演算法)的實現細節,彼此完全獨立。

三、代碼實現

需求:商場收費系統,根據商品的單價和數量,得出的總結可以"正常收費", "打八折","滿300返100", "打七折", "打五折"等等。

代碼如下:

abstract class CashSuper   {       public abstract double acceptCash(double money);   }   //正常收費子類   class CashNormal : CashSuper   {       public override double acceptCash(double money)       {           return money;       }   }   //打折收費子類   class CashRebate : CashSuper   {       private double moneyRebate = 1d;       public CashRebate(string moneyRebate)       {           this.moneyRebate = double.Parse(moneyRebate);       }       public override double acceptCash(double money)       {           return money*moneyRebate;       }   }   //返利收費子類   class CashReturn : CashSuper   {       private double moneyCondition = 0.0d;       private double moneyReturn = 0.0d;       public CashReturn (string moneyCondition,string moneyReturn)       {           this.moneyCondition=double.Parse(moneyCondition);           this.moneyReturn=double.Parse(moneyReturn);       }       public override double acceptCash(double money)       {           double result = money;           if (money >= moneyCondition)               result = money - Math.Floor(money / moneyCondition) * moneyReturn;           return result;       }   }

 

Context類:

class CashContext    {        private CashSuper cs;        public CashContext(string type)        {            switch (type)            {                 case "正常收費":                    CashNormal cs0=new CashNormal();                    cs=cs0;                    break;                case "滿300返100":                    CashReturn cr1 = new CashReturn("300","100");                    cs = cr1;                    break;                case "打八折":                    CashRebate cr2 = new CashRebate("0.8");                    cs = cr2;                    break;            }                    }        public double GetResult(double money)        {            return cs.acceptCash(money);        }    }

 

用戶端調用:

CashContext csuper = new CashContext(cbxType.SelectedItem.ToString());double totalPrices = 0d;totalPrices = csuper.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));

 

四、和工廠的區別:

上面代碼也可以用Factory 方法實現,不過策略和工廠的區別很大。

如果用工廠實現,核心工廠是這樣的,其他地方代碼不變:

class CashFactory    {        public static CashSuper createCashAccept(string type)        {            CashSuper cs = null;            switch (type)            {                 case "正常收費":                    cs = new CashNormal();                    break;                case "滿300返100":                    cs = new CashReturn("300", "100");                    break;                case "打八折":                    cs = new CashRebate("0.8");                    break;            }            return cs;                    }    }

使用工廠的時候,用戶端調用

CashSuper csuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString());            
double totalPrices = 0d;//原廠模式解決方案totalPrices = csuper.acceptCash(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));

可見兩種實現,他們代碼很類似,大概的只是多了一個方法

public double GetResult(double money){return cs.acceptCash(money);}

由此可見他們的區別:他們返回的內容不同,工廠是返回不同的執行個體給用戶端,而策略的執行個體只是為了自己的類使用,他是根據參數返回演算法的結果GetResult,因此他們區別是目的不同,一個是為了返回執行個體,一個是為了封裝不同的演算法,適應演算法的變化。

聯繫我們

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