设计模式-简单工厂模式

    简单工厂模式:简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。

      (一)简单工厂模式的特点:

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

      (二)简单工厂模式的使用情况:
        1. 工厂类负责创建的对象比较少;
        2. 客户只知道传入工厂类的参数,对于如何创建对象(逻辑)不关心;
        3. 由于简单工厂很容易违反高内聚责任分配原则,因此一般只在很简单的情况下应用。
 
       (三)简单工厂模式的优点:
       工厂类是整个模式的关键。包含了必要的逻辑判断,根据外界给定的信息,决定究竟应该创建哪个具体类的对象。通过使用工厂类,外界可以从直接创建具体产品对象的尴尬局面摆脱出来,仅仅需要负责“消费”对象就可以了。而不必管这些对象究竟如何创建及如何组织的。明确了各自的职责和权利,有利于整个软件体系结构的优化。
 
       (四)简单工厂模式的缺点:
        1. 由于工厂类集中了所有实例的创建逻辑,违反了高内聚责任分配原则,将全部创建逻辑集中到了一个工厂类中;它所能创建的类只能是事先考虑到的,如果需要添加新的类,则就需要改变工厂类了。
        2. 当系统中的具体产品类不断增多时候,可能会出现要求工厂类根据不同条件创建不同实例的需求.这种对条件的判断和对具体产品类型的判断交错在一起,很难避免模块功能的蔓延,对系统的维护和扩展非常不利;
        这些缺点在工厂方法模式中得到了一定的克服
 
        (五)简单工厂模式的角色
       在装饰模式中的各个角色有:
  (1)工厂(Creator)角色:简单工厂模式的核心,它负责实现创建所有实例的内部逻辑。工厂类可以被外界直接调用,创建所需的产品对象。
  (2)抽象产品(Product)角色:简单工厂模式所创建的所有对象的父类,它负责描述所有实例所共有的公共接口。
  (3)具体产品(Concrete Product)角色:是简单工厂模式的创建目标,所有创建的对象都是充当这个角色的某个具体类的实例。
       (六)示例
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 简单工厂模式
 7 {
 8     public abstract class Operation
 9     {
10         private double numberA = 0d;
11         public double NumberA
12         {
13             get { return numberA; }
14             set { numberA = value; }
15         }
16 
17         private double numberB = 0d;
18         public double NumberB
19         {
20             get { return numberB; }
21             set { numberB = value; }
22         }
23 
24         public abstract double GetResult();
25     }
26 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 简单工厂模式
 7 {
 8     class OperationAdd:Operation
 9     {
10         public override double GetResult()
11         {
12             return NumberA + NumberB;
13         }
14     }
15 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 简单工厂模式
 7 {
 8     class OperationSub:Operation
 9     {
10         public override double GetResult()
11         {
12             return NumberA - NumberB;
13         }
14     }
15 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 简单工厂模式
 7 {
 8     class OperationMul:Operation
 9     {
10         public override double GetResult()
11         {
12             return NumberA * NumberB;
13         }
14     }
15 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 简单工厂模式
 7 {
 8     class OperationDiv:Operation
 9     {
10         public override double GetResult()
11         {
12             if (NumberB != 0)
13             {
14                 return NumberA / NumberB;
15             }
16             else
17             {
18                 throw new Exception("除数不能为零");
19             }
20         }
21     }
22 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 简单工厂模式
 7 {
 8     /// <summary>
 9     /// 通过工厂,只要输入运算符号,就可以实例化出正确的对象
10     /// </summary>
11     class OperationFactory
12     {
13         public Operation GetOperation(string operate)
14         {
15             Operation operation = null;
16             switch (operate)
17             {
18                 case "+":
19                     operation = new OperationAdd();
20                     break;
21                 case "-":
22                     operation = new OperationSub();
23                     break;
24                 case "*":
25                     operation = new OperationMul();
26                     break;
27                 case "/":
28                     operation = new OperationDiv();
29                     break;
30             }
31             return operation;
32         }
33     }
34 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 简单工厂模式
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             while (true)
13             {
14                 try
15                 {
16                     Console.WriteLine("输入第一个数:");
17                     double numberA = Convert.ToDouble(Console.ReadLine());
18                     Console.WriteLine("输入运算符号:");
19                     string operate = Console.ReadLine();
20                     Console.WriteLine("输入第二个数:");
21                     double numberB = Convert.ToDouble(Console.ReadLine());
22                     OperationFactory factory = new OperationFactory();
23                     Operation operation = factory.GetOperation(operate);
24                     operation.NumberA = numberA;
25                     operation.NumberB = numberB;
26                     Console.WriteLine("结果是:" + operation.GetResult());
27                 }
28                 catch(Exception e)
29                 {
30                     Console.WriteLine(e.Message);
31                     Console.ReadKey();
32                     Console.Clear();
33                     continue;
34                 }
35             }
36         }
37     }
38 }

 

posted @ 2014-10-13 14:43  徘徊在路上  阅读(148)  评论(0编辑  收藏  举报