簡單原廠模式c#,

來源:互聯網
上載者:User

簡單原廠模式c#,

簡單原廠模式c#上簡單例子

namespace simplefactory

{
    public partial class Form1 : Form
    {
        private void button1_Click(object sender, EventArgs e)//使用者層次
        {
            Operation operation = OperationFactory.createOperate(this.cbbOperation.SelectedIndex);//串連到簡單工廠擷取演算法
            operation.NumberA = Convert.ToDouble(this.txtNumberOne.Text);//對簡單工廠得來的演算法進行賦值
            operation.NumberB = Convert.ToDouble(this.txtNumberTwo.Text);

            this.txtResult.Text = operation.getResult().ToString();//擷取演算法結果

        }

    }
     class OperationFactory//簡單工廠層次
    {
        public static Operation createOperate(int operate)//工廠內分加法和減法兩種
        {
            Operation oper = null;
            switch (operate)
            {
                case 0:
                    {
                        oper = new OperationAdd();//加法
                        break;
                    }
                case 1:
                    {
                        oper = new OperationSub();//減法
                        break;
                    }
            }
            return oper;
        }
    }
    public abstract class Operation//對加減法演算法時行抽象,以方便統一賦值NumberA、NumberB及計算結果getResult()
    {
        public double NumberA { get; set; }
        public double NumberB { get; set; }
        public abstract double getResult();
    }
    class OperationAdd : Operation//加法演算法繼承抽象演算法
    {
        public override double getResult()
        {
            return NumberA + NumberB;
        }
    }
    class OperationSub : Operation//減法演算法繼承抽象演算法
    {
        public override double getResult()
        {
            return NumberA - NumberB;


        }
    }
}

聯繫我們

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