面向对象的好处:可维护,可复用,可扩展,灵活性好。
高内聚,低耦合
简单工厂模式 例子
1、基类-运算类
2、加法类,减法类,乘法类,除法类为运算类的子类
3、子类分别实现基类的虚函数
4、工厂类,通过create方法产生运算符
/// <summary>
/// 运算类工厂
/// </summary>
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;
}
}
Operation oper;//基类
oper = OperationFactory.createOperate("+");//工厂的使用,生产一个运算符,参数为加号
oper.NumberA = Convert.ToDouble(strNumberA);
oper.NumberB = Convert.ToDouble(strNumberB);
int result= oper.GetResult().ToString();