大话设计模式之简单工厂

1.面向对象的好处

容易维护,容易扩展,容易复用

 

2.复制vs复用

尽可能避免重复,也就说把重复的代码提取出来

 

3.业务的封装

业务逻辑与界面逻辑分开,让她们之间的耦合度下降,只有分离开,才可以达到容易维护或扩展

 

4.紧耦合vs松耦合

如果要修改任何一个算法,就不需要提供其他的算法的代码

 

5.简单工厂模式

主页面
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂
 8 {
 9     class OperationFactory
10     {
11         /// <summary>
12         /// 选择页面
13         /// </summary>
14         /// <param name="operate"></param>
15         /// <returns></returns>
16         public static Operation createOperate(string operate) {
17             Operation oper = null;
18 
19             switch (operate)
20             {
21                 case "+":
22                     oper = new OperationAdd();
23                     break;
24                 case "-":
25                     oper = new OperationSub();
26                     break;
27                 case "*":
28                     oper = new OperationDiv();
29                     break;
30                 case "/":
31                     oper = new OperationDiv();
32                     break;
33                 default:
34                     break;
35             }
36             return oper;
37         }
38     }
39 }
选择(逻辑代码)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂
 8 {
 9     class OperationFactory
10     {
11         /// <summary>
12         /// 选择页面
13         /// </summary>
14         /// <param name="operate"></param>
15         /// <returns></returns>
16         public static Operation createOperate(string operate) {
17             Operation oper = null;
18 
19             switch (operate)
20             {
21                 case "+":
22                     oper = new OperationAdd();
23                     break;
24                 case "-":
25                     oper = new OperationSub();
26                     break;
27                 case "*":
28                     oper = new OperationDiv();
29                     break;
30                 case "/":
31                     oper = new OperationDiv();
32                     break;
33                 default:
34                     break;
35             }
36             return oper;
37         }
38     }
39 }
除数(逻辑代码)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂
 8 {
 9     class OperationMul:Operation
10     {
11         public override double GetResult()
12         {
13             double result = 0;
14             result = NumberA * NumberB;
15             return result;
16         }
17     }
18 }
乘法(逻辑代码)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂
 8 {
 9     class OperationSub:Operation
10     {
11         public override double GetResult()
12         {
13             double result = 0;
14             result = NumberA - NumberB;
15             return result;
16         }
17     }
18 }
减法(逻辑代码)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂
 8 {
 9     class OperationAdd:Operation
10     {
11         public override double GetResult()
12         {
13             double result = 0;
14             result = NumberA + NumberB;
15             return result;
16         }
17     }
18 }
加法(逻辑)
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂
 8 {
 9     class Operation
10     {
11         private double _numberA = 0;
12 
13         public double NumberA
14         {
15             get { return _numberA; }
16             set { _numberA = value; }
17         }
18 
19         private double _numberB = 0;
20 
21         public double NumberB
22         {
23             get { return _numberB; }
24             set { _numberB = value; }
25         }
26 
27         public virtual double GetResult() {
28             double result = 0;
29             return result;
30         }
31     }
32 }
运算类(父类)

 

posted on 2017-03-22 10:37  瞿亮  阅读(254)  评论(0编辑  收藏  举报

导航