设计模式01-简单工厂模式

  1. using System;
  2. using System.Collections.Generic;
    using System.Text;
  3.  namespace 简单工厂模式
  4. {
  5.     /// <summary>
  6.     /// 运算类的抽象类
  7.     /// </summary>
  8.     public class Operation
  9.     {
  10.         /// <summary>
  11.         /// 参与运算数
  12.         /// </summary>
  13.         private double _numberA = 0;
  14.         /// <summary>
  15.         /// 参与运算数
  16.         /// </summary>
  17.         private double _numberB = 0;
  18.  
  19.         public double NumberA
  20.         {
  21.             get
  22.             {
  23.                 return _numberA;
  24.             }
  25.             set
  26.             {
  27.                 this._numberA = value;
  28.             }
  29.         }
  30.         public double NumberB
  31.         {
  32.             get
  33.             {
  34.                 return this._numberB;
  35.             }
  36.             set
  37.             {
  38.                 this._numberB = value;
  39.             }
  40.         }
  41.         /// <summary>
  42.         /// 返回计算结果,这里是虚拟的,让不同的运算法则类来实现
  43.         /// </summary>
  44.         /// <returns></returns>
  45.         public virtual double GetResult()
  46.         {
  47.             double result = 0;
  48.             return result;
  49.         }
  50.     }
  51.     /// <summary>
  52.     /// 继承抽象类并实现加法运算的类
  53.     /// </summary>
  54.    public class OperationAdd : Operation
  55.     {
  56.         public override double GetResult()
  57.         {
  58.             double result = 0;
  59.             result = this.NumberA + this.NumberB;
  60.             return result;
  61.         }
  62.     }
  63.     /// <summary>
  64.     /// 继承抽象类并实现减法运算的类
  65.     /// </summary>
  66.    public class OperationSub : Operation
  67.     {
  68.         public override double GetResult()
  69.         {
  70.             double result = 0;
  71.             result = this.NumberA - this.NumberB;
  72.             return result;
  73.         }
  74.     }
  75.     /// <summary>
  76.     /// 继承抽象类并实现乘法运算的类
  77.     /// </summary>
  78.    public class OperationMul : Operation
  79.     {
  80.         public override double GetResult()
  81.         {
  82.             double result = 0;
  83.             result = this.NumberA * this.NumberB;
  84.             return result;
  85.         }
  86.     }
  87.     /// <summary>
  88.     /// 继承抽象类,并实现除法运算的类
  89.     /// </summary>
  90.    public class OperationDiv : Operation
  91.     {
  92.         public override double GetResult()
  93.         {
  94.             double result = 0;
  95.             if (this.NumberB == 0)
  96.                 throw new Exception("除数不能为0");
  97.             result = this.NumberA / this.NumberB;
  98.             return result;
  99.         }
  100.     }
  101.    
  102.     #region
  103.     //各种计算的算法,我们已经完成了,那么我们该如何知道去实例化哪个类呢?这里用到了简单工厂模式
  104.     //简单工厂模式:它是一个单独的类,可以根据不同的条件(这里指操作符)完成不同实例的创造。
  105.     #endregion
  106.     /// <summary>
  107.     /// 运算工厂类,实现了不同算法类的实例化
  108.     /// </summary>
  109.     public class OperationFactory
  110.     {
  111.         public static Operation createOperate(string operate)
  112.         {
  113.             Operation oper = null;
  114.             switch (operate)
  115.             {
  116.                case "+":
  117.                    oper = new OperationAdd();
  118.                    break;
  119.                 case "-":
  120.                     oper = new OperationSub();
  121.                     break;
  122.                 case "*":
  123.                     oper = new OperationMul();
  124.                     break;
  125.                 case "/":
  126.                     oper = new OperationDiv();
  127.                     break;
  128.  
  129.             }
  130.             return oper;
  131.         }
  132.     }
  133.  
  134.     public class Program
  135.     {
  136.         public  static void   Main()
  137.         {
  138.             Operation oper;
  139.             //输入合适的运算符号,工厂就实例化出合适的对象,通过多态返回父类的方式实现计算结果
  140.             oper = OperationFactory.createOperate("+");
  141.             oper.NumberA = 1;
  142.             oper.NumberB = 2;
  143.             double result = oper.GetResult();
  144.             Console.WriteLine(result.ToString());
  145.             Console.ReadLine();
  146.         }
  147.     }
  148. }
  149.  
posted @ 2008-06-17 14:28  kewin  阅读(162)  评论(0编辑  收藏  举报