工厂方法模式
开放封闭原则:是说软件实体(类,模块,函数等等)应该可以扩展,但是不可修改。
核心
定义一个用于创建对象的接口,让子类决定实例化哪一个类。
好处
工厂方法模式是简单工厂模式的衍生,解决了许多简单工厂模式的问题。首先完全实现‘开放封闭原则’,实现了可扩展。其次更复杂的层次结构,可以应用于产品结果复杂的场合。工厂方法模式对简单工厂模式进行了抽象。有一个抽象的Factory类(工厂的工厂),这个类将不再负责具体的产品生产,而是只制定一些规范,具体的生产工作由其子类去完成。
例子
情景是:简单计算器,我们输入2个数和一个符号得到运算结果。
首先使用简单工厂模式来实现
//客户端代码
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.NunberA = Convert.ToDouble(strNumberA);
oper.NunberB = Convert.ToDouble(strNumberB);
strResult = Convert.ToString(oper.GetResult());
Console.WriteLine("结果是:" + strResult);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("输入非法字符。请输入数字" + ex.Message);
}
}
}
//运算父类
public class Operation
{
private double _numberA = 0;
private double _numberB = 0;
public double NunberA { get; set; }
public double NunberB { get; set; }
public virtual double GetResult()
{
double result = 0;
return result;
}
}
//运算子类
class OperationAdd : Operation
{
public override double GetResult()
{
double result = 0;
result = NunberA + NunberB;
return result;
}
}
class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NunberA - NunberB;
return result;
}
}
class OperationMult : Operation
{
public override double GetResult()
{
double result = 0;
result = NunberA * NunberB;
return result;
}
}
class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
if (NunberB == 0)
throw new Exception("除数不能为0!");
result = NunberA / NunberB;
return result;
}
}
//简单工厂类
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 OperationMult();
break;
case "/":
oper = new OperationDiv();
break;
}
return oper;
}
}
然后我们用工厂方法模式实现:我们省略了运算子类和父类,只集中处理不同部分:
//工厂接口
interface IFactory
{
Operation CreateOperation();
}
//接口实现类
class AddFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationAdd();
}
}
class SubFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationSub();
}
}
class MultFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationMult();
}
}
class DivFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationDiv();
}
}
//客户端
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 = "";
switch (strOperate)
{
case "+":
IFactory operFactory = new AddFactory();
Operation oper = operFactory.CreateOperation();
oper.NunberA = Convert.ToDouble(strNumberA);
oper.NunberB = Convert.ToDouble(strNumberB);
strResult = Convert.ToString(oper.GetResult());
break;
case "-":
IFactory operFactory1 = new SubFactory();
break;
case "*":
IFactory operFactory2 = new MultFactory();
break;
case "/":
IFactory operFactory3 = new DivFactory();
break;
}
Console.WriteLine("结果是:" + strResult);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("输入非法字符。请输入数字" + ex.Message);
}
}