close

image

開放封閉原則: 對於擴展是開放的(Open for extension),對於更改是封閉的(Closed for modification)

建立抽象來隔離以後發生的同類變化:

1. 可擴展性: Operaotor父類別中的GetResult(),抽象化四則運算不同變化

2. 可複用性: 四則運算子類別分別繼承Operator父類別

3. 可維護性: 工廠模式減低用戶端呼叫的負擔,只需要傳入不同mode,就會傳出適合的運算實體。

運算類別: Operaotor

   1: public class Operator
   2:     {
   3:         private double m_x;
   4:         private double m_y;
   5:         public double X {
   6:             get { return m_x; }
   7:             set { m_x = value; }
   8:         }
   9:         public double Y
  10:         {
  11:             get { return m_y; }
  12:             set { m_y = value; }
  13:         }
  14:         public virtual double GetResult()
  15:         {
  16:             double result = 0;
  17:             return result;
  18:         }
  19:     }

運算子類別: +

   1: class OperatorAdd : Operator
   2:     {
   3:         public override double GetResult()
   4:         {
   5:             double result = 0;
   6:             result = base.X + base.Y;
   7:             return result;
   8:         }
   9:     }

運算子類別:-

   1: class OperatorSub : Operator
   2:   {
   3:       public override double GetResult()
   4:       {
   5:           double result = 0;
   6:           result = base.X - base.Y;
   7:           return result;
   8:       }
   9:   }

運算子類別:*

   1: class OperatorMul: Operator
   2:    {
   3:        public override double GetResult()
   4:        {
   5:            double result = 0;
   6:            result = base.X * base.Y;
   7:            return result;
   8:        }
   9:    }

運算子類別:/

   1: class OperatorDev:Operator
   2: {
   3:     public override double GetResult()
   4:     {
   5:         double result;
   6:         if (base.Y == 0)
   7:             throw new Exception("Cannot divided by zero!");
   8:         result = base.X / base.Y;
   9:         return result;
  10:     }
  11: }
運算類別工廠
   1: public class OperatorFactory
   2:     {
   3:         public static Operator createOperator(string op)
   4:         {
   5:             Operator m_op = null;
   6:             switch (op)
   7:             {
   8:                 case "+":
   9:                     m_op = new OperatorAdd();
  10:                     break;
  11:                 case "-":
  12:                     m_op = new OperatorSub();
  13:                     break;
  14:                 case "*":
  15:                     m_op = new OperatorMul();
  16:                     break;
  17:                 case "/":
  18:                     m_op = new OperatorDev();
  19:                     break;
  20:             }
  21:             return m_op;
  22:         }
  23:  
  24:     }

用戶端程式碼

   1: private void button1_Click(object sender, EventArgs e)
   2:         {
   3:             DP.Operator op;
   4:             op = DP.OperatorFactory.createOperator(comboBox1.Text);
   5:             op.X = Convert.ToDouble(textBox1.Text);
   6:             op.Y = Convert.ToDouble(textBox2.Text);
   7:             try {
   8:                 this.Text = op.GetResult().ToString();
   9:             } catch(Exception ex)
  10:             {
  11:                 MessageBox.Show(ex.Message.ToString());
  12:             }
  13:             
  14:         }

測試+

image

測試-

image

測試*

image

測試/0

image


參考資料:

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

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


延伸閱讀:

策略與簡單工廠結合

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

    天天向上

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