个人认为的好处有:
1,增加有共同的方法和属性的事物,不会修改已有的事物.
2,结构清晰明了.
从一个计算器的例子里我们可以看出:
一个抽象类,加上四个操作运算类,最后一个工厂类.
Operation
public abstract class Operation
{
protected double numberA;
protected double numberB;
private double result = 0;
public Operation()
{
}
public Double NumberA
{
set
{
this.numberA = value;
}
}
public Double NumberB
{
set
{
this.numberB = value;
}
}
public virtual Double Result
{
get
{
return result;
}
}
}
public abstract class Operation
{
protected double numberA;
protected double numberB;
private double result = 0;
public Operation()
{
}
public Double NumberA
{
set
{
this.numberA = value;
}
}
public Double NumberB
{
set
{
this.numberB = value;
}
}
public virtual Double Result
{
get
{
return result;
}
}
}
Operator
public class OperationAdd:Operation
{
public override double Result
{
get
{
return base.numberA + base.numberB;
}
}
}
public class OperationSub : Operation
{
public override double Result
{
get
{
return base.numberA - base.numberB;
}
}
}
public class OperationMul : Operation
{
public override double Result
{
get
{
return base.numberA * base.numberB;
}
}
}
public class OperationDiv : Operation
{
public override double Result
{
get
{
if (base.numberB == 0)
{
throw new Exception("The divisor can not zero");
}
return base.numberA / base.numberB;
}
}
}
public class OperationAdd:Operation
{
public override double Result
{
get
{
return base.numberA + base.numberB;
}
}
}
public class OperationSub : Operation
{
public override double Result
{
get
{
return base.numberA - base.numberB;
}
}
}
public class OperationMul : Operation
{
public override double Result
{
get
{
return base.numberA * base.numberB;
}
}
}
public class OperationDiv : Operation
{
public override double Result
{
get
{
if (base.numberB == 0)
{
throw new Exception("The divisor can not zero");
}
return base.numberA / base.numberB;
}
}
}
Factory
public class OperationFactory
{
public static Operation CreateOperationFactory(string operate)
{
Operation operation = null;
switch (operate)
{
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/":
operation = new OperationDiv();
break;
}
return operation;
}
}
public class OperationFactory
{
public static Operation CreateOperationFactory(string operate)
{
Operation operation = null;
switch (operate)
{
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/":
operation = new OperationDiv();
break;
}
return operation;
}
}
static
void
Main(
string
[] args)
{
Console.WriteLine("Please enter a digit A:");
string strNumberA = Console.ReadLine();
Console.WriteLine("Please enter a operator(+,-,*,/):");
string strOperator = Console.ReadLine();
Console.WriteLine("Please enter a digit B:");
string strNumberB = Console.ReadLine();
Operation operation = OperationFactory.CreateOperationFactory(strOperator);
operation.NumberA = Double.Parse(strNumberA);
operation.NumberB = Double.Parse(strNumberB);
Console.WriteLine(string.Format("{0} {1} {2} = {3}", strNumberA, strOperator,strNumberB, operation.Result));
}
{
Console.WriteLine("Please enter a digit A:");
string strNumberA = Console.ReadLine();
Console.WriteLine("Please enter a operator(+,-,*,/):");
string strOperator = Console.ReadLine();
Console.WriteLine("Please enter a digit B:");
string strNumberB = Console.ReadLine();
Operation operation = OperationFactory.CreateOperationFactory(strOperator);
operation.NumberA = Double.Parse(strNumberA);
operation.NumberB = Double.Parse(strNumberB);
Console.WriteLine(string.Format("{0} {1} {2} = {3}", strNumberA, strOperator,strNumberB, operation.Result));
}