标签:工厂模式
简单工厂
简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。
例如:简单工厂类
public class OperationFactory
{
public static Operation createOperator(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;
}
return oper;
}
}
有一个操作类OPeration里面有numberA,和numberB两个成员,有一个虚方法getResult()。类OperationAdd,OperationSub,OperationMul和OperationDIv继承OPeration类。
客户端代码:
Operation oper;
oper=OperationFactory.createOperate("+");
oper.NumberA=1;
oper.NumberB=2;
double result=oper.GetResult;
标签:工厂模式
原文地址:http://blog.csdn.net/wangfengfan1/article/details/46524107