黄梅戏严凤英全集:设计模式1:Simple Factory Pattern(简单工厂模式)

来源:百度文库 编辑:九乡新闻网 时间:2024/05/02 07:57:40

设计模式1:Simple Factory Pattern(简单工厂模式)

分类: 设计模式 2009-06-24 17:54 209人阅读 评论(0) 收藏 举报

原文:http://www.cnblogs.com/zhenyulu/articles/36462.html

工厂模式专门负责将大量有共同接口的类实例化。工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类。工厂模式有以下几种形态:

简单工厂(Simple Factory)模式

工厂方法(Factory Method)模式

抽象工厂(Abstract Factory)模式

 

一、 简单工厂(Simple Factory)模式

Simple Factory模式根据提供给它的数据,返回几个可能类中的一个类的实例。通常它返回的类都有一个公共的父类和公共的方法。

Simple Factory模式实际上不是GoF 23个设计模式中的一员。

 

二、 Simple Factory模式角色与结构:



工厂类角色Creator (LightSimpleFactory):工厂类在客户端的直接控制下(Create方法)创建产品对象。

抽象产品角色Product (Light):定义简单工厂创建的对象的父类或它们共同拥有的接口。可以是一个类、抽象类或接口。

具体产品角色ConcreteProduct (BulbLight, TubeLight):定义工厂具体加工出的对象。

 

三、 程序举例:

    1. using System;

    2. public abstract class Light
    3. {
    4.    public abstract void TurnOn();
    5.    public abstract void TurnOff();
    6. }

    7. public class BulbLight : Light
    8. {
    9.    public override void TurnOn()
    10.    {
    11.       Console.WriteLine("Bulb Light is Turned on");
    12.    }

    13.    public override void TurnOff()
    14.    {
    15.       Console.WriteLine("Bulb Light is Turned off");
    16.    }
    17. }

    18. public class TubeLight : Light
    19. {
    20.    public override void TurnOn()
    21.    {
    22.       Console.WriteLine("Tube Light is Turned on");
    23.    }

    24.    public override void TurnOff()
    25.    {
    26.       Console.WriteLine("Tube Light is Turned off");
    27.    }
    28. }

    29. public class LightSimpleFactory
    30. {
    31.    public Light Create(string LightType)
    32.    {
    33.       if(LightType == "Bulb")
    34.          return new BulbLight();
    35.       else if(LightType == "Tube")
    36.          return new TubeLight();
    37.       else
    38.          return null;
    39.    }
    40. }

    41. public class Client
    42. {
    43.    public static void Main()
    44.    {
    45.       LightSimpleFactory lsf = new LightSimpleFactory();

    46.       Light l = lsf.Create("Bulb");
    47.       l.TurnOn();
    48.       l.TurnOff();

    49.       Console.WriteLine("-----------------");

    50.       l = lsf.Create("Tube");
    51.       l.TurnOn();
    52.       l.TurnOff();
    53.    }
    54. }

    四、 Simple Factory模式演化

    Simple Factory模式演化(一)

    除了上面的用法外,在有些情况下Simple Factory可以由抽象产品角色扮演,一个抽象产品类同时是子类的工厂。

    程序举例:

    1. using System;

    2. public class Light
    3. {
    4.    public virtual void TurnOn()
    5.    {
    6.    }

    7.    public virtual void TurnOff()
    8.    {
    9.    }

    10.    public static Light Create(string LightType)
    11.    {
    12.       if(LightType == "Bulb")
    13.          return new BulbLight();
    14.       else if(LightType == "Tube")
    15.          return new TubeLight();
    16.       else
    17.          return null;
    18.    }
    19. }

    20. public class BulbLight : Light
    21. {
    22.    public override void TurnOn()
    23.    {
    24.       Console.WriteLine("Bulb Light is Turned on");
    25.    }

    26.    public override void TurnOff()
    27.    {
    28.       Console.WriteLine("Bulb Light is Turned off");
    29.    }
    30. }

    31. public class TubeLight : Light
    32. {
    33.    public override void TurnOn()
    34.    {
    35.       Console.WriteLine("Tube Light is Turned on");
    36.    }

    37.    public override void TurnOff()
    38.    {
    39.       Console.WriteLine("Tube Light is Turned off");
    40.    }
    41. }

    42. public class Client
    43. {
    44.    public static void Main()
    45.    {
    46.       Light l = Light.Create("Bulb");
    47.       l.TurnOn();
    48.       l.TurnOff();

    49.       Console.WriteLine("-----------------");

    50.       l = Light.Create("Tube");
    51.       l.TurnOn();
    52.       l.TurnOff();
    53.    }
    54. }


      Simple Factory模式演化(二)

      三个角色全部合并:

       

      与单件模式(Singleton)相近,但是有区别。

      五、 优点与缺点:

      优点:
      工厂类含有必要的判断逻辑,可以决定在什么时候创建哪一个产品类的实例,客户端可以免除直接创建产品对象的责任,而仅仅"消费"产品。简单工厂模式通过这种做法实现了对责任的分割。

      缺点:
      当产品有复杂的多层等级结构时,工厂类只有自己,以不变应万变,就是模式的缺点。因为工厂类集中了所有产品创建逻辑,一旦不能正常工作,整个系统都要受到影响。

      同时,系统扩展困难,一旦添加新产品就不得不修改工厂逻辑,有可能造成工厂逻辑过于复杂。

      另外,简单工厂模式通常使用静态工厂方法,这使得无法由子类继承,造成工厂角色无法形成基于继承的等级结构。

       



      参考文献:
      阎宏,《Java与模式》,电子工业出版社
      [美]James W. Cooper,《C#设计模式》,电子工业出版社
      [美]Alan Shalloway  James R. Trott,《Design Patterns Explained》,中国电力出版社
      [美]Robert C. Martin,《敏捷软件开发-原则、模式与实践》,清华大学出版社
      [美]Don Box, Chris Sells,《.NET本质论 第1卷:公共语言运行库》,中国电力出版社