close


image

image

   1: abstract class CashObj
   2: {
   3:     public abstract double acceptCash(double money);
   4: }


上面定義了抽象方法accecptCash, 因此衍生類別必須override此方法

[2][C#] 區分 abstract、virtual、override 和 new

abstract 抽象方法,是空的方法,沒有方法實體,派(衍)生類必須以 override 實現此方法。

virtual 虛擬方法,若希望預料到基礎類別的這個方法在將來的派(衍)生類別中會被覆寫(override 或 new),則此方法必須被聲明為 virtual。

無折扣

   1: // 無折扣
   2: class CashNormal : CashObj
   3: {
   4:     public override double acceptCash(double money)
   5:     {
   6:         return money;
   7:     }
   8: }

打九折

   1: // 打九折
   2: class CashRebate : CashObj
   3: {
   4:     private double ratio;
   5:     
   6:     // 建構子
   7:     public CashRebate(string strRatio)
   8:     {
   9:         this.ratio = double.Parse(strRatio);
  10:     }
  11:     public override double acceptCash(double money)
  12:     {
  13:         return money * this.ratio;
  14:     }
  15: }

滿1000送100

   1: // 滿1000送100
   2:     class CashCoupon : CashObj
   3:     {
   4:         private double moneyLowerBound = 0.0d;
   5:         private double moneyReturn = 100;
   6:         
   7:         //建構子
   8:         public CashCoupon(string moneyLowerBound, string moneyReturn)
   9:         {
  10:             this.moneyLowerBound = double.Parse(moneyLowerBound);
  11:             this.moneyReturn = double.Parse(moneyReturn);
  12:         }
  13:         public override double acceptCash(double money)
  14:         {
  15:             double result = money;
  16:             if (money >= moneyLowerBound)
  17:                 result = money - Math.Floor(money / moneyLowerBound) * moneyReturn;
  18:             return result;
  19:         }
  20:     }

策略與簡單工廠結合

1. 根據折扣模式(mode), 工廠就實體化出合適的物件給m_cashObj: 無折扣(CashNormal),打九折(CashRebate),滿1000送100(CashRebate)其中一種


2.以相同的方法(GetResult)調用所有的演算法(CashNormal、CashRebate、CashRebate),減少了各種演算法類別與使用演算法類別之間的耦合

3. 選擇具體物件實現的職責,由Context來承擔(減輕用戶端來判斷),這是因為策略模式+簡單工廠模式的緣故

   1: class CashStrategy
   2:     {
   3:        
   4:         CashObj m_cashObj = null;
   5:         
   6:         // 建構子
   7:         public CashStrategy(int mode)
   8:         {
   9:             switch(mode)
  10:             {
  11:                 case 0:    // 無則扣
  12:                     CashNormal cn = new CashNormal();
  13:                     m_cashObj = cn;
  14:                     break;
  15:                 case 1:    // 打九折         
  16:                     CashRebate cr = new CashRebate("0.9");
  17:                     m_cashObj = cr;
  18:                     break;
  19:                 case 2:    // 滿1000送100
  20:                     CashCoupon cc = new CashCoupon("1000", "100");
  21:                     m_cashObj = cc;
  22:                     break;
  23:             }
  24:  
  25:  
  26:         }
  27:         public double GetResult(double money)
  28:         {
  29:             return m_cashObj.acceptCash(money);
  30:         }
  31:     }

客戶端程式

   1: private void button1_Click(object sender, EventArgs e)
   2:       {
   3:           CashStrategy cs = new CashStrategy(comboBox1.SelectedIndex);
   4:           double money = double.Parse(textBox1.Text);
   5:           double result = cs.GetResult(money);
   6:           textBox2.Text = result.ToString();
   7:       }

測試1:

image

測試2:

image

測試3:

image


參考資料:

1. 大話設計模式, 作者:程杰

2. [C#] 區分 abstract、virtual、override 和 new

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()