首页 > 代码库 > 步步为营-09-简单工厂类-计算器

步步为营-09-简单工厂类-计算器

先把整体UI建好

技术分享

1 分析需要个计算的基类,一个构造函数,两个属性,一个方法

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calcultor
{
   public abstract class Calculate
    {
        public Calculate(double a,double b) {
            this.FirstNumber = a;
            this.SecondNumber = b;
        }
        public double FirstNumber { get; set; }
        public double SecondNumber { get; set; }

        public abstract double Result();
    }
}
Calculate

2 定义子类+-*/

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calcultor
{
    class A:Calculate
    {
         public A(double  a, double b) : base(a,b)
        {
            this.FirstNumber = a;
            this.SecondNumber = b;


        }
        public override double Result()
        {
            return this.FirstNumber + this.SecondNumber;
        }
    }
}
+
技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calcultor
{
    class S:Calculate
    {
         public S(double  a, double b) : base(a,b)
        {
            this.FirstNumber = a;
            this.SecondNumber = b;
        }
        public override double Result()
        {
            return this.FirstNumber - this.SecondNumber;
        }
    }
}
-
技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calcultor
{
    class M:Calculate
    {
         public M(double  a, double b) : base(a,b)
        {
            this.FirstNumber = a;
            this.SecondNumber = b;
        }
        public override double Result( )
        {
            return this.FirstNumber * this.SecondNumber;
        }
    }
}
*
技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calcultor
{
    class D:Calculate
    {
         public D(double  a, double b) : base(a,b)
        {
            this.FirstNumber = a;
            this.SecondNumber = b;
        }
        public override double Result( )
        {
            if (this.SecondNumber != 0)
            {
                return this.FirstNumber / this.SecondNumber;
            }
            return 0;
        }
    }
}
/

3 定义抽象工厂

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calcultor
{
   public  class Factory
    {
       public static Calculate GetResult(string oper,double a,double b)
       {
           Calculate ca = null;
           switch (oper)
           {
               case "+":
                   ca = new A(a,b);
                   break;
               case "-":
                   ca = new S(a, b);
                   break;
               case "*":
                   ca = new M(a, b);
                   break;
               case "/":
                   ca = new D(a, b);
                   break;
           }
           return ca;
       }

    }
}
Factory

4 Main方法

技术分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calcultor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string op = btnAdd.Text;
            GetResult(op);
        }

     

        private void btnSub_Click(object sender, EventArgs e)
        {
            string op = btnSub.Text;
            GetResult(op);
        }

        private void btnMul_Click(object sender, EventArgs e)
        {
            string op = btnMul.Text;
            GetResult(op);
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            string op = btnDiv.Text;
            GetResult(op);
        }
        private void GetResult(string op)
        {
            double a = Convert.ToDouble(txtFirst.Text);
            double b = Convert.ToDouble(txtSecond.Text);
            Calculate c = Factory.GetResult(op, a, b);
            lblResult.Text = c.Result().ToString();
        }
    }
}
Main

5 运行效果

技术分享

步步为营-09-简单工厂类-计算器