简单工厂模式和工厂方法模式的区别

简单工厂模式:本着高内聚低耦合的原则,将系统的逻辑部分和功能分开

例如简易计算器的实现代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 
  5 namespace Operation
  6 {
  7     /// <summary>
  8     /// 运算类
  9     /// </summary>
 10     public class Operation
 11     {
 12         private double _numberA = 0;
 13         private double _numberB = 0;
 14         
 15         /// <summary>
 16         /// 数字A
 17         /// </summary>
 18         public double NumberA
 19         {
 20             get
 21             {
 22                 return _numberA;
 23             }
 24             set
 25             {
 26                 _numberA = value;
 27             }
 28         }
 29 
 30         /// <summary>
 31         /// 数字B
 32         /// </summary>
 33         public double NumberB
 34         {
 35             get
 36             {
 37                 return _numberB;
 38             }
 39             set
 40             {
 41                 _numberB = value;
 42             }
 43         }
 44 
 45         /// <summary>
 46         /// 得到运算结果
 47         /// </summary>
 48         /// <returns></returns>
 49         public virtual double getResult()
 50         {
 51             double result = 0; 
 52             return result;
 53         }
 54 
 55        
 56     }
 57 
 58     /// <summary>
 59     /// 加法类
 60     /// </summary>
 61     class OperationAdd : Operation
 62     {
 63         public override double getResult()
 64         {
 65             double result = 0; 
 66             result = NumberA + NumberB;
 67             return result;
 68         }
 69     }
 70 
 71     /// <summary>
 72     /// 减法类
 73     /// </summary>
 74     class OperationSub : Operation
 75     {
 76        public override double getResult()
 77         {
 78             double result = 0;
 79             result = NumberA - NumberB;
 80             return result;
 81         }
 82     }
 83 
 84     /// <summary>
 85     /// 乘法类
 86     /// </summary>
 87     class OperationMul : Operation
 88     {
 89         public override double getResult()
 90         {
 91             double result = 0;
 92             result = NumberA * NumberB;
 93             return result;
 94         }
 95     }
 96 
 97     /// <summary>
 98     /// 除法类
 99     /// </summary>
100     class OperationDiv : Operation
101     {
102         public override double getResult()
103         {
104             double result = 0;
105             if (NumberB==0)
106                 throw new Exception("除数不能为0。");
107             result = NumberA / NumberB;
108             return result;
109         }
110     }
111 
112     /// <summary>
113     /// 平方类
114     /// </summary>
115     class OperationSqr : Operation
116     {
117         public override double getResult()
118         {
119             double result = 0;
120             result = NumberB * NumberB;
121             return result;
122         }
123     }
124 
125     /// <summary>
126     /// 平方根类
127     /// </summary>
128     class OperationSqrt : Operation
129     {
130         public override double getResult()
131         {
132             double result = 0;
133             if (NumberB < 0)
134                 throw new Exception("负数不能开平方根。");
135             result = Math.Sqrt(NumberB);
136             return result;
137         }
138     }
139 
140     /// <summary>
141     /// 相反数类
142     /// </summary>
143     class OperationReverse : Operation
144     {
145         public override double getResult()
146         {
147             double result = 0;
148             result = -NumberB;
149             return result;
150         }
151     }
152 
153     /// <summary>
154     /// 运算类工厂
155     /// </summary>
156     class OperationFactory
157     {
158         public static Operation createOperate(string operate)
159         {
160             Operation oper = null;
161             switch (operate)
162             {
163                 case "+":
164                     {
165                         oper = new OperationAdd();
166                         break;
167                     }
168                 case "-":
169                     {
170                         oper = new OperationSub();
171                         break;
172                     }
173                 case "*":
174                     {
175                         oper = new OperationMul();
176                         break;
177                     }
178                 case "/":
179                     {
180                         oper = new OperationDiv();
181                         break;
182                     }
183                 case "sqr":
184                     {
185                         oper = new OperationSqr();
186                         break;
187                     }
188                 case "sqrt":
189                     {
190                         oper = new OperationSqrt();
191                         break;
192                     }
193                 case "+/-":
194                     {
195                         oper = new OperationReverse();
196                         break;
197                     }
198             }
199 
200             return oper;
201         }
202     }
203 
204 }
View Code

 

工厂方法模式:和工厂方法不同的地方是加入了“开放-封闭原则”(软件实体类、模块或者函数等等,应该可以扩展,但是不可以修改)规则,将简单工厂的内部判断逻辑移动到了客户端代码来进行,在扩展新功能的时候,简单工厂模式要修改工厂类,工厂方法模式是修改客户端。

计算器实现代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 
  5 namespace 工厂方法_计算器
  6 {
  7 
  8     /// <summary>
  9     /// 运算类
 10     /// </summary>
 11     class Operation
 12     {
 13         private double _numberA = 0;
 14         private double _numberB = 0;
 15 
 16         public double NumberA
 17         {
 18             get { return _numberA; }
 19             set { _numberA = value; }
 20         }
 21 
 22         public double NumberB
 23         {
 24             get { return _numberB; }
 25             set { _numberB = value; }
 26         }
 27 
 28         /// <summary>
 29         /// 得到运算结果
 30         /// </summary>
 31         /// <returns></returns>
 32         public virtual double GetResult()
 33         {
 34             double result = 0;
 35             return result;
 36         }
 37     }
 38 
 39     /// <summary>
 40     /// 加法类
 41     /// </summary>
 42     class OperationAdd : Operation
 43     {
 44         public override double GetResult()
 45         {
 46             double result = 0;
 47             result = NumberA + NumberB;
 48             return result;
 49         }
 50     }
 51 
 52     /// <summary>
 53     /// 减法类
 54     /// </summary>
 55     class OperationSub : Operation
 56     {
 57         public override double GetResult()
 58         {
 59             double result = 0;
 60             result = NumberA - NumberB;
 61             return result;
 62         }
 63     }
 64     /// <summary>
 65     /// 乘法类
 66     /// </summary>
 67     class OperationMul : Operation
 68     {
 69         public override double GetResult()
 70         {
 71             double result = 0;
 72             result = NumberA * NumberB;
 73             return result;
 74         }
 75     }
 76     /// <summary>
 77     /// 除法类
 78     /// </summary>
 79     class OperationDiv : Operation
 80     {
 81         public override double GetResult()
 82         {
 83             double result = 0;
 84             if (NumberB == 0)
 85                 throw new Exception("除数不能为0。");
 86             result = NumberA / NumberB;
 87             return result;
 88         }
 89     }
 90 
 91     /// <summary>
 92     /// 工厂方法
 93     /// </summary>
 94     interface IFactory
 95     {
 96         Operation CreateOperation();
 97     }
 98 
 99     /// <summary>
100     /// 专门负责生产“+”的工厂
101     /// </summary>
102     class AddFactory : IFactory
103     {
104         public Operation CreateOperation()
105         {
106             return new OperationAdd();
107         }
108     }
109 
110     /// <summary>
111     /// 专门负责生产“-”的工厂
112     /// </summary>
113     class SubFactory : IFactory
114     {
115         public Operation CreateOperation()
116         {
117             return new OperationSub();
118         }
119     }
120 
121     /// <summary>
122     /// 专门负责生产“*”的工厂
123     /// </summary>
124     class MulFactory : IFactory
125     {
126         public Operation CreateOperation()
127         {
128             return new OperationMul();
129         }
130     }
131 
132     /// <summary>
133     /// 专门负责生产“/”的工厂
134     /// </summary>
135     class DivFactory : IFactory
136     {
137         public Operation CreateOperation()
138         {
139             return new OperationDiv();
140         }
141     }
142 
143 }
View Code

 

参考资料:程杰《大话设计模式》

 

posted @ 2016-03-29 16:53  图哈特  阅读(426)  评论(0编辑  收藏  举报