标签:因此 enter 创建型 ret operation 优点 convert ide hub
简单工厂模式(Simple Factory Pattern):又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。简单来说,也就是由一个工厂类根据传入的参数决定创建出哪一种产品类的实例。
(图片来自博客:https://blog.csdn.net/xingjiarong/article/details/49999121)
简单工厂模式主要有三个部分组成:
三、代码示例
此处代码分为二个版本迭代,第二个版本为最终版本,可以直接查看。代码实现的功能仅仅是数字之间的加减法。
版本一:此版本将业务逻辑与界面逻辑分开:
/// <summary> /// 该版本将运算类封装,不考虑输入错误的情况 /// </summary> public class VersionOne { public void GetResult() { try { Console.WriteLine("请输入数字A:"); string strNumberA = Console.ReadLine(); Console.WriteLine("请选择运算符号(+、-、*、/)"); string strOperate = Console.ReadLine(); Console.WriteLine("请输入数字B:"); string strNumberB = Console.ReadLine(); string strResult = ""; strResult = Convert.ToString(OperationVerOne.GetResult(Convert.ToDouble(strNumberA), Convert.ToDouble(strNumberB), strOperate)); Console.WriteLine("结果是:" + strResult); Console.ReadKey(); } catch (Exception e) { Console.WriteLine("您输入的有错" + e.Message); } } } public class OperationVerOne { public static double GetResult(double numberA, double numberB, string operate) { double result = 0d; switch (operate) { case "+": result = numberA + numberB; break; case "-": result = numberA - numberB; break; case "*": result = numberA * numberB; break; case "/": result = numberA / numberB; break; } return result; } }
版本二, 通过继承的方式实现加减乘除运算分离的操作遵守开放--封闭的原则,并通过工厂实现运算:
class VersionTwo
{
public void Operation() { OperationTwo oper; Console.WriteLine("请输入数字A:"); string strNumberA = Console.ReadLine(); Console.WriteLine("请选择运算符号(+、-、*、/)"); oper = OperationFactory.createOperation(Console.ReadLine()); Console.WriteLine("请输入数字B:"); string strNumberB = Console.ReadLine(); oper.NumberA = Convert.ToDouble(strNumberA); oper.NumberB = Convert.ToDouble(strNumberB); double result = oper.GetResult(); Console.WriteLine("结果是: " + result); Console.ReadKey(); } } // 工厂类
// 通过传入的参数创建对应的操作类
public class OperationFactory { public static OperationTwo createOperation(string operate) { OperationTwo oper = null; switch (operate) { case "+": oper = new OperationAdd(); break; case "-": oper = new OperationSub(); break; case "*": oper = new OperationMul(); break; case "/": oper = new OperationDiv(); break; } return oper; } } // Operation 类,基类 public class OperationTwo { private double _numberA = 0; private double _numberB = 0; public double NumberA { get { return _numberA; } set { this._numberA = value; } } public double NumberB { get { return _numberB; } set { this._numberB = value; } } public virtual double GetResult() { double result = 0; return result; } } // 加减乘除类 class OperationAdd : OperationTwo { public override double GetResult() { double result = 0; result = this.NumberA + this.NumberB; return result; } } class OperationSub : OperationTwo { public override double GetResult() { double result = 0; result = NumberA - NumberB; return result; } } class OperationMul : OperationTwo { public override double GetResult() { double result = 0; result = NumberA * NumberB; return result; } } class OperationDiv : OperationTwo { public override double GetResult() { double result = 0; result = NumberA / NumberB; return result; } }
该示例中的UML 图
代码的github:https://github.com/gp112/DesignPattern
参考:
《大话设计模式》
博客:https://blog.csdn.net/xingjiarong/article/details/49999121
标签:因此 enter 创建型 ret operation 优点 convert ide hub
原文地址:https://www.cnblogs.com/gp112/p/11072348.html