c#设计模式之简单工厂

1.面向对象的3大属性,封装、继承、多态,以一个加单的计算机为例:

创建一个父类Operation 有两个属性 和一个计算方法(虚方法),便于子类重写:

  1  public class Operation
  2     {
  3         private double _numberA = 0;
  4         private double _numberB = 0;
  5 
  6         public double NumberA
  7         {
  8             get { return _numberA; }
  9             set { _numberA = value; }
 10         }
 11 
 12         public double NumberB
 13         {
 14             get { return _numberB; }
 15             set { _numberB = value; }
 16         }
 17 
 18         /// <summary>虚方法
 19         /// 
 20         /// </summary>
 21         /// <returns></returns>
 22         public virtual double GetResult()
 23         {
 24             double result = 0;
 25             return result;
 26         }
 27     }
View Code

接下来创建计算类,加减乘除类来集成操作类

加类

  1  public class OperationAdd:Operation
  2     {
  3         public override double GetResult()
  4         {
  5             double result = 0;
  6             result = NumberA + NumberB;
  7             return result;
  8         }
  9     }
View Code

减类

  1  public class OperationSub:Operation
  2     {
  3         public override double GetResult()
  4         {
  5             double result = 0;
  6             result = NumberA - NumberB;
  7             return result;
  8         }
  9     }
View Code

乘类

  1  public class OperationMul:Operation
  2     {
  3         public override double GetResult()
  4         {
  5             double result = 0;
  6             result = NumberA * NumberB;
  7             return result;
  8         }
  9     }
View Code

除类

  1  public class OperationDiv:Operation
  2     {
  3         public override double GetResult()
  4         {
  5             double result = 0;
  6             if (NumberB == 0)
  7                 throw new Exception("除数不能为0");
  8             result = NumberA / NumberB;
  9             return result;
 10         }
 11     }
View Code

然后通过简单工厂类来创建计算的实例

  1  public class OperationFactory
  2     {
  3         public static Operation createOperate(string operate)
  4         {
  5             Operation oper = null;
  6             switch (operate)
  7             {
  8                 case "+":
  9                     oper = new OperationAdd();
 10                     break;
 11                 case "-":
 12                     oper = new OperationSub();
 13                     break;
 14                 case "*":
 15                     oper = new OperationMul();
 16                     break;
 17                 case "/":
 18                     oper = new OperationDiv();
 19                     break;
 20             }
 21             return oper;
 22         }
 23     }
View Code

最后通过界面去调用:

  1  static void Main(string[] args)
  2         {
  3             Operation oper;
  4             oper = OperationFactory.createOperate("+");
  5             oper.NumberA = 1;
  6             oper.NumberB = 2;
  7             double result = oper.GetResult();
  8             Console.WriteLine(result);
  9             Console.ReadKey();
 10         }
View Code

这里把计算过程都封装成一个方法,每个计算类又集成了操作类,这样由于每个方法都重写了计算方法,这里表现出一个方法的不同表现形态,如果以后要增加其他的方法,只需要继承操作类 重写计算方法即可,这样也不会对原有的程序进行修改,实现了解耦,面向对象语言的优点在于可维护性,可复用,可扩展、灵活性。

以上为自学的一点总结,希望有不对的地方,请大家指点,谢谢

posted @ 2016-08-10 10:07  坚持的阳光  阅读(556)  评论(0编辑  收藏  举报