简单工厂模式
代码
public class Operation
{
private double _numberA = 0;
private double _numberB = 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;
}
}
class OperationAdd : Operation
{
public override double GetResult()
{
double result =0;
result = NumberA + NumberB;
return result;
}
}
class OperationASub : Operation
{
public override double GetResult()
{
double result =0;
result = NumberA - NumberB;
return result;
}
}
其它类似 所有具体操作类都继承基类 Operation
public class OperationFactory
{
public static Operation CreateOperate(string operate)
{
Operation oper = null;
switch(operate)
{
case "+":
oper = new OperationAdd();
break;
case "-":
oper = new OperationSub();
break;
}
return oper;
}
}
---客户端代码---
Operation oper;
oper = OperationFactory.createOperate("+");
oper.NumberA = 1;
oper.NumberB = 2;
double result = oper.GetResult();
图