大話設計模式讀書筆記1——簡單工廠模式

Rising_Sun發表於2014-08-15

最近幾日,重溫了一下《大話設計模式》這本書,當時讀的倉促,有很多沒有注意的地方,現在仔細翻看起來,發現這值得細細品味的一本書!!

好東西就要記下來!!!

第一章筆記:從一個簡單的計算器程式來看簡單工廠模式。

變化的地方就要封裝,用一個單獨的類來做創造例項的過程這就是工廠。

UML圖:

    /// <summary>
    /// 運算類
    /// </summary>
    public class Operation
    {
        public double Number1 { get; set; }

        public double Number2 { get; set; }

        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }
    /// <summary>
    /// 加法類
    /// </summary>
    public class OprerationAdd : Operation
    {
        public override double GetResult()
        {
            double reslut = 0;
            reslut = Number1 + Number2;
            return reslut;
        }
    }

    /// <summary>
    /// 減法類
    /// </summary>
    public class OprerationSub : Operation
    {
        public override double GetResult()
        {
            double reslut = 0;
            reslut = Number1 - Number2;
            return reslut;
        }
    }

    /// <summary>
    /// 乘法類
    /// </summary>
    public class OprerationMul : Operation
    {
        public override double GetResult()
        {
            double reslut = 0;
            reslut = Number1 * Number2;
            return reslut;
        }
    }

    /// <summary>
    /// 除法類
    /// </summary>
    public class OprerationDiv : Operation
    {
        public override double GetResult()
        {
            double reslut = 0;
            if (Number2==0)
            {
                throw new Exception("除數不能等於0");
            }
            reslut = Number1 / Number2;
            return reslut;
        }
    }

運算工廠類:

public class OperationFactory
    {
        public static Operation CreateOperate(string oper)
        {
            Operation operation = null;
            switch (oper)
            {
                case "+":
                    operation = new OprerationAdd();
                    break;
                case "-":
                    operation = new OprerationSub();
                    break;
                case "*":
                    operation = new OprerationMul();
                    break;
                case "/":
                    operation = new OprerationDiv();
                    break;
            }
            return operation;
        }
    }

客戶端程式碼:

   public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            this.cbxOper.SelectedIndex = 0;         
        }     
        
        /// <summary>
        /// 運算點選
        /// </summary> 
        private void btnCompute_Click(object sender, EventArgs e)
        {
            double num1 = 0;
            double num2 = 0;
            if (!double.TryParse(this.txtNum1.Text,out num1))
            {
                this.lblInfo.Text = "請輸入正確的數字";
                this.txtNum1.Focus();
                return;
            }

            if (!double.TryParse(this.txtNum2.Text, out num2))
            {
                this.lblInfo.Text = "請輸入正確的數字";
                this.txtNum2.Focus();
                return;
            }

            Operation operation = null;
            operation = OperationFactory.CreateOperate(this.cbxOper.Text);
            operation.Number1 = num1;
            operation.Number2 = num2;
            double ret = operation.GetResult();
            this.txtResult.Text = ret.ToString();
        }
    }

介面顯示:

-=原始碼下載=-

 

相關文章