一、前言
简单工厂是一个负责生产对象的中间类,例如有加减乘除四个运算方法,它们继承父类,并重写父类的方法,简单工厂根据不同的运算符创建不同的实例对象赋值给父类,实现了面向对象的另一个原则——降低对象之间的耦合度。简单工厂模式解决了客户端直接依赖于具体对象的问题,客户端可以消除直接创建对象的责任,而仅仅是消费产品。简单工厂模式实现了对责任的分割。
简单工厂模式的缺点:
- 工厂类集中了所有产品创建逻辑,一旦不能正常工作,整个系统都会受到影响
- 系统扩展困难,一旦添加新产品就不得不修改工厂逻辑,这样就会造成工厂逻辑过于复杂。
了解了简单工厂模式之后的优缺点之后,我们之后就可以知道简单工厂的应用场景了:
- 当工厂类负责创建的对象比较少时可以考虑使用简单工厂模式
- 客户如果只知道传入工厂类的参数,对于如何创建对象的逻辑不关心时可以考虑使用简单工厂模式
二、简单工厂结构图
三、实例代码
1.1运算类
/// <summary> /// 运算类 /// </summary> public class Operation { private double _numberA = 0; private double _numberB = 0; /// <summary> /// 数字A /// </summary> public double NumberA { get { return _numberA; } set { _numberA = value; } } /// <summary> /// 数字B /// </summary> public double NumberB { get { return _numberB; } set { _numberB = value; } } /// <summary> /// 得到运算结果 /// </summary> /// <returns></returns> public virtual double GetResult() { double result = 0; return result; } /// <summary> /// 检查输入的字符串是否准确 /// </summary> /// <param name="currentNumber"></param> /// <param name="inputString"></param> /// <returns></returns> public static string checkNumberInput(string currentNumber, string inputString) { string result = ""; if (inputString == ".") { if (currentNumber.IndexOf(".") < 0) { if (currentNumber.Length == 0) result = "0" + inputString; else result = currentNumber + inputString; } } else if (currentNumber == "0") { result = inputString; } else { result = currentNumber + inputString; } return result; } } /// <summary> /// 加法类 /// </summary> class OperationAdd : Operation { public override double GetResult() { double result = 0; result = NumberA + NumberB; return result; } } /// <summary> /// 减法类 /// </summary> class OperationSub : Operation { public override double GetResult() { double result = 0; result = NumberA - NumberB; return result; } } /// <summary> /// 乘法类 /// </summary> class OperationMul : Operation { public override double GetResult() { double result = 0; result = NumberA * NumberB; return result; } } /// <summary> /// 除法类 /// </summary> class OperationDiv : Operation { public override double GetResult() { double result = 0; if (NumberB == 0) throw new Exception("除数不能为0。"); result = NumberA / NumberB; return result; } } /// <summary> /// 平方类 /// </summary> class OperationSqr : Operation { public override double GetResult() { double result = 0; result = NumberB * NumberB; return result; } } /// <summary> /// 平方根类 /// </summary> class OperationSqrt : Operation { public override double GetResult() { double result = 0; if (NumberB < 0) throw new Exception("负数不能开平方根。"); result = Math.Sqrt(NumberB); return result; } } /// <summary> /// 相反数类 /// </summary> class OperationReverse : Operation { public override double GetResult() { double result = 0; result = -NumberB; return result; } }
1.2简单工厂
/// <summary> /// 运算类工厂 /// </summary> public 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 OperationMul(); break; } case "/": { oper = new OperationDiv(); break; } case "sqr": { oper = new OperationSqr(); break; } case "sqrt": { oper = new OperationSqrt(); break; } case "+/-": { oper = new OperationReverse(); break; } } return oper; } }
1.3客户端程序
class Program { static void Main(string[] args) { try { Console.Write("请输入数字A:"); string strNumberA = Console.ReadLine(); Console.Write("请选择运算符号(+、-、*、/):"); string strOperate = Console.ReadLine(); Console.Write("请输入数字B:"); string strNumberB = Console.ReadLine(); string strResult = ""; Operation oper; oper = OperationFactory.createOperate(strOperate); oper.NumberA = Convert.ToDouble(strNumberA); oper.NumberB = Convert.ToDouble(strNumberB); strResult = oper.GetResult().ToString(); Console.WriteLine("结果是:" + strResult); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("您的输入有错:" + ex.Message); } } }
时间: 2024-11-10 12:11:04