简单工厂设计模式实现计算器的案例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单工厂模式
{
    class Program
    {
        static void Main(string[] args)
        {
            //控制台提示信息输出
            System.Console.Write("  请输入数据A: ");

            //开始接受键盘的数据输入并且存入内存
            string strNumberA = Console.ReadLine();

            //在控制台输出选择运算符的提示信息
            Console.Write("  请选择运算符号(+、-、*、/): ");

            //开始接受提示的运算符并且存入对应内存
            string strOperate = Console.ReadLine();

            //运算符第二个数字输入提示
            Console.Write("  请输入数字B:  ");

            //开始从io设备接受流数据并且存入内存
            string strNumberB = Console.ReadLine();

            //申明运算符对象基类,保存具体运算符对象子类
            Operation oper;

            //try是为了防止被代码段抛出的异常直接终止应用程序,不友好
            //我在catch进行异常捕捉,并且输出异常的原因,方便我们程序员去调试
            try    
            {

                //既然我已经从io流得到了运算符比如+以及进行运算的2个数据
                //我们现在主要就是得到对应运算符的对象并且把2个数据传给这个对象
                //我们通过一个运算符工厂类专门生产运算符对象,根据我们传过来的参数
                //比如告诉我们运算符是+,就给我们造一个+对象
                oper = OperationFactory.createOperate(strOperate);

                //准备给运算符对象输入需要的信息
                oper.NumberA = double.Parse(strNumberA);
                oper.NumberB = double.Parse(strNumberB);

                //我们直接通过当前运算符对象得到结果
                double result = oper.GetResult();
                Console.WriteLine("计算结果为:" + result);
            }
            /我们知道可能会抛除数为0异常,我们就写个对应异常捕捉代码
            catch (DivideByZeroException e)  
            {

                Console.WriteLine(e.Message);
            }

            Console.WriteLine("测试用.");
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单工厂模式
{
    class Operation
    {
        private double _numberA = 0.0;  //用来保存调方传过来的参数
        private double _numberB = 0.0;

        //提供对外设置和得到当前字段的属性
        public double NumberA
        {
            get { return _numberA; }
            set { _numberA = value; }
        }

        public double NumberB
        {
            get { return _numberB; }
            set { _numberB = value; }
        }

        //运算符基类对象的接口,用于得到结果 并且标志为可以重写
        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }      
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单工厂模式
{
    //加运算对象
    class OperationAdd:Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA + NumberB;
            return result;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单工厂模式
{
     //运算符对象生产工厂
    class OperationFactory
    {
        public static Operation createOperate(string operate)
        {
            Operation oper = null;
            switch (operate)
            {
                case "+":
                    oper = new OperationAdd();
                    break;
                case "-":
                    oper = new OperationSub();
                    break;
                case "/":
                    oper = new OperationDiv();
                    break;
                default:
                    break;
            }

            return oper;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单工厂模式
{
    class OperationSub:Operation
    {
        public override double GetResult()
        {
            double result = 0.0;
            result = NumberA - NumberB;
            return result;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单工厂模式
{
    class OperationDiv:Operation
    {
        public override double GetResult()
        {
            double result = 0.0;
            if(Math.Abs(NumberB) < 0.000000001)
            {
                throw new DivideByZeroException("除数不能出现为0。");
            }
            result = NumberA / NumberB;

            return result;
        }
    }
}

简单工厂设计模式的优点分析:

1.由于我们的具体设置那种运算,比如对2个数求和,相乘我们直接就增加一个对应的子类,我们增加了功能,但是又没有修改原来的代码,符合开放封闭原则。

2.我们把显示逻辑和业务逻辑进行了分离,那么我们说的显示逻辑是什么呢,比如我们通过业务运算后得到了计算结果,然后我们需要把结果输出到控制台。

这个输出代码我们可以看成是显示逻辑,我们肯定是没有必要把显示逻辑代码和具体运算结果的代码也就是业务逻辑组合在一起,我们计算逻辑单独编码,显示逻辑或者叫界面代码单独编码。
这样分开的话,我们在其他项目需要这个计算代码是可以直接用的。

3.由于我们引进了工厂类专门生产对应的运算符对象,因此假如我们把工厂类以及具体每个运算对象是程序员A写的代码,而程序员B仅仅只是准备了从io流接受用户输入的运算符数据以及运算操作符。
这个时候我们不想写具体运算代码,那么我们就用程序员A的现成代码,我们只需要调用程序员A的工厂类并且告诉他我需要什么运算符对象,这个工厂类就给我们造一个对应的对象。
并且我们把数据传给这个对象。那么我们就可以通过这个对象拿到结果。
这个时候我们的程序员B就可以直接输出结果。
如果我们程序员B想实现2个数的平方运算。这个时候我们不需要改我们的代码,我们直接输入对应的运算符, 那么程序员A就需要在工厂类里增加平方运算符的一个分支,并且写一个平方运算符类即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛掰是怎么形成的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值