用简单工厂设计模式实现WinForm计算器

后台代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;



namespace CalclatorTest

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void Form1_Load(object sender, EventArgs e)

        {

            //界面加载的时候显示加减乘除符号

            cbo.Items.Add("+");

            cbo.Items.Add("-");

            cbo.Items.Add("*");

            cbo.Items.Add("/");

        }



        private void btnResult_Click(object sender, EventArgs e)

        {

            //cbo.SelectedItem获取当前选定的项 返回值是object的 需要做下显示转换为string类型的

            string fuhao = (string)cbo.SelectedItem;

            //调用工厂这个静态方法去选择用哪个计算器,返回一个计算器的类型引用

           Cale cale = Factory(fuhao);

           double num1=Convert.ToDouble(txtNum1.Text);

            double num2=Convert.ToDouble(txtNum2.Text);

            //通过计算器对象的引用调用对应的方法 体现多态的思想

            lblResult.Text = cale.GetResult(num1, num2).ToString();

        }



        static Cale Factory(string fuhao)

        {

            Cale cale=null;

            switch (fuhao)

            { 

                case "+":

                    cale = new AddCale();

                    break;

                case "-":

                    cale = new SubCale();

                    break;

                case "*":

                    cale = new MultiCale();

                    break;

                case "/":

                    cale = new DivCale();

                    break;

            }

            return cale;

        }



        private void txtNum1_KeyPress(object sender, KeyPressEventArgs e)

        {

            //在数字文本框中按下按键的时候触发事件

            char key = e.KeyChar;//e为事件提供参数

            if (key >= 48 & key <= 57 || key == 8) //判断是不是字母A-Z 或退格键

            {



            }

            else

            {

                e.Handled = true;  //否则无法录入

            }

        }



        private void txtNum2_KeyPress(object sender, KeyPressEventArgs e)

        {

            char key = e.KeyChar;

            if (key >= 48 & key <= 57 || key == 8)

            {



            }

            else

            {

                e.Handled = true;

            }

        }



    }



    abstract class Cale

    {

        public double Num1

        {

            get;

            set;

        }

        public double Num2

        {

            get;

            set;

        }

        public abstract double GetResult(double num1,double num2);

    }



    class AddCale:Cale

    {

        public override double GetResult(double num1, double num2)

        {

            return num1 + num2;

        }

    }



    class SubCale : Cale

    {

        public override double GetResult(double num1, double num2)

        {

            return num1 - num2;

        }

    }



    class MultiCale : Cale

    {

        public override double GetResult(double num1, double num2)

        {

            return num1 * num2;

        }

    }



    class DivCale : Cale

    {

        public override double GetResult(double num1, double num2)

        {

            return num1 / num2;

        }

    }

}

压缩文件:Cale.rar

你可能感兴趣的:(WinForm)