【设计模式】创建型模式之简单工厂模式(一)

简单工厂模式

简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类(这些产品类继承自一个父类或接口)的实例。【来自于百度百科】

简单的理解为:创建一个工厂类,而这个工厂类会根据传入的参数,创建不同的类实例。

为何有简单工厂?

业务逻辑与界面分开,降低耦合度。

应用场景:当你的客户端不需要知道具体类的名字,具体类是如何实现,我只需要知道对外公布的一个类,并提供一个简单的调用带参数的方法,把参数传入对应的方法,可以返回一个相对应的实例,并得到对应的结果。这个时候你就可以考虑使用“简单工厂模式”。

举个例子

  在大话设计模式中,最典型的一个例子:计算器

定义一个运算类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _01简单工厂模式
 8 {
 9     //定义一个运算类,并提供两个数字进行计算
10     public class Operation
11     {
12         private double _numberA = 0;
13         private double _numberB = 0;
14         public double NumberA
15         {
16             get { return _numberA; }
17             set { _numberA = value; }
18         }
19         public double NumberB
20         {
21             get { return _numberB; }
22             set { _numberB = value; }
23         }
24         public virtual double GtetResult()
25         {
26             double result = 0;
27             return result;
28         }
29     }
30 }

加法类,并继承 Operation 类,重写父类的方法

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _01简单工厂模式
 8 {
 9     class OperationAdd : Operation
10     {
11         //重写父类方法,实现加法运算
12         public override double GtetResult()
13         {
14             double result = 0;
15             result = NumberA + NumberB;
16             return result;
17         }
18     }
19 }

减法类,并继承 Operation 类,重写父类的方法

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _01简单工厂模式
 8 {
 9     class OperationSub:Operation
10     {
11         //重写父类方法,实现减法运算
12         public override double GtetResult()
13         {
14             double result = 0;
15             result = NumberA - NumberB;
16             return result;
17         }
18     }
19 }

乘法类,并继承 Operation 类,重写父类的方法

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _01简单工厂模式
 8 {
 9     class OperationMul : Operation
10     {
11         //重写父类方法,实现乘法运算
12         public override double GtetResult()
13         {
14             double result = 0;
15             result = NumberA * NumberB;
16             return result;
17         }
18     }
19 }

除法类,并继承 Operation 类,重写父类的方法

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _01简单工厂模式
 8 {
 9     class OperationDiv:Operation
10     {
11         //重写父类方法,实现除法运算
12         public override double GtetResult()
13         {
14             double result = 0;
15             if (NumberB == 0)
16                 throw new Exception("除数不能0。");
17             result = NumberA / NumberB;
18             return result;
19         }
20     }
21 }

 这时候是我们最重要的一步:工厂类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _01简单工厂模式
 8 {
 9     //工厂类
10     public class OperationFactory
11     {
12         //返回类型Operation
13         public static Operation createOperate(string operate)
14         {
15             Operation oper = null;
16             switch (operate)
17             {
18                 case "+":
19                     //实例加法类
20                     oper = new OperationAdd();
21                     break;
22                 case "-":
23                     //实例减法类
24                     oper = new OperationSub();
25                     break;
26                 case "*":
27                     //实例乘法类
28                     oper = new OperationMul();
29                     break;
30                 case "/":
31                     //实例除法类
32                     oper = new OperationDiv();
33                     break;
34                 default:
35                     break;
36             }
37             return oper;
38         }
39     }
40 }

客户端实现,客户端,定义 Operation 并传递对应的参数,计算出对应的结果

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _01简单工厂模式
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //定义Operation,并传递+号
14             Operation oper;
15             oper = OperationFactory.createOperate("+");
16             oper.NumberA = 1;
17             oper.NumberB = 2;
18             //获取结果
19             double result = oper.GtetResult();
20             //输出结果
21             Console.WriteLine(result.ToString());
22             Console.ReadLine();
23         }
24     }
25 }

 

模式分析

将对象的创建和对象本身业务处理分离可以降低系统的耦合度,使得两者修改起来都相对容易

优点:客户端无需创建具体的“对象

  1. 减少与客户端依赖性
  2. 代码复用
  3. 可维护、可复用、可扩展

缺点:最大的问题在于工厂类的职责相对过重,增加新的产品需要修改工厂类的判断逻辑,这一点与面向对象的开闭原则是相违背的。

  1. 系统扩展难,新需求则改工厂逻辑
  2. 工厂类为重要部分,一旦工厂倒闭,后续将无法继续
posted @ 2018-06-21 23:48  嗷大喵学编程  阅读(186)  评论(0)    收藏  举报