您的位置:首页 > 其它

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

2017-11-17 02:16 585 查看
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就需要在工厂类里增加平方运算符的一个分支,并且写一个平方运算符类即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: