我所理解的设计模式(C++实现)——抽象工厂模式(Abstract Factory Pattern)

来源:互联网 发布:金针软件下载 编辑:程序博客网 时间:2024/06/07 19:15

解决的问题:

       在系统里a,b,c三个组件必须同时使用,但是a的同类 a1和a2这三种方法有共同特点但是是互斥的,b,b1,b2和c,c1,c2和a/a1/a2是一样的。就比如说创建在不同操作系统的视窗环境下都能够运行的系统时,Unix下面有unixButton和 unixText,Win下面也有winButton和winText,unixButton和unixText必须在一个系统unix里面用,而winButton和winText只能在Win下面用。但是winButton和unixButton这两种东西都是有相同的特点的,比如说按下去之后会触发事件,比如说他上面有文字描述等等,但是winButton和unixButton却又是不可以混用的。

     那么此问题就可以用抽象工厂很好的解决:
     在抽象工厂模式中如何选择使用 winButton ,winText,有具体的工厂类winFactory来负责,因为他们含有选择合适的产品对象的逻辑,所以是与应用系统的商业逻辑紧密相关的。而抽象工厂类来负责定义接口,他才是抽象工厂模式的核心。
     而winButton/macButton则是一种产品族,有共同的特点,他们具体特点有抽象产品类或者接口来定义和描述。但是他们具体的实现有具体的产品类负责,这些是客户端最终想要的东西,所以其内部一定充满了应用系统的商业逻辑(触发逻辑/样式逻辑等)。

类图结构:    



样例实现:

[cpp] view plaincopy
  1. // CplusplusAbstractFactory.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include<typeinfo>  
  6. // "AbstractProductA" 草食动物  
  7. class Herbivore  
  8. {  
  9. };  
  10.   
  11. // "AbstractProductB" 食肉动物  
  12. class Carnivore  
  13. {  
  14. public:  
  15.     // Methods  
  16.     virtual void Eat( Herbivore *h ) {};  
  17. };  
  18.   
  19. // "ProductA1"  
  20. class Wildebeest : public Herbivore  
  21. {  
  22. };  
  23.   
  24. // "ProductA2"  
  25. class Bison : public Herbivore  
  26. {  
  27. };  
  28.   
  29. // "ProductB1"  
  30. class Lion : public Carnivore  
  31. {  
  32.   
  33. public:  
  34.     // Methods  
  35.     void Eat( Herbivore *h )  
  36.     {  
  37.         // eat wildebeest      
  38.         printf("Lion eats %s\n",typeid(h).name());  
  39.     }  
  40. };  
  41.   
  42. // "ProductB2"  
  43. class Wolf : public Carnivore  
  44. {  
  45.   
  46. public:  
  47.     // Methods  
  48.     void Eat( Herbivore *h )  
  49.     {  
  50.         // Eat bison  
  51.         printf("Wolf eats %s\n",typeid(h).name());  
  52.     }  
  53. };  
  54.   
  55. // "AbstractFactory"  
  56. class ContinentFactory  
  57. {  
  58. public:  
  59.     // Methods  
  60.     virtual Herbivore* CreateHerbivore()  
  61.     {  
  62.         return new Herbivore();  
  63.     }  
  64.     virtual Carnivore* CreateCarnivore()  
  65.     {  
  66.         return new Carnivore();  
  67.     }  
  68. };  
  69.   
  70. // "ConcreteFactory1"  
  71. class AfricaFactory : public ContinentFactory  
  72. {  
  73. public:  
  74.     // Methods  
  75.     Herbivore* CreateHerbivore()  
  76.     {   
  77.         return new Wildebeest();   
  78.     }  
  79.   
  80.     Carnivore* CreateCarnivore()  
  81.     {   
  82.         return new Lion();   
  83.     }  
  84. };  
  85.   
  86. // "ConcreteFactory2"  
  87. class AmericaFactory : public ContinentFactory  
  88. {  
  89. public:  
  90.     // Methods  
  91.     Herbivore* CreateHerbivore()  
  92.     {   
  93.         return new Bison();   
  94.     }  
  95.   
  96.     Carnivore* CreateCarnivore()  
  97.     {   
  98.         return new Wolf();   
  99.     }  
  100. };  
  101.   
  102.   
  103.   
  104. // "Client"  
  105. class AnimalWorld  
  106. {  
  107. private:  
  108.     // Fields  
  109.     Herbivore* herbivore;  
  110.     Carnivore* carnivore;  
  111.   
  112. public:  
  113.     // Constructors  
  114.     AnimalWorld( ContinentFactory *factory )  
  115.     {  
  116.         carnivore = factory->CreateCarnivore();  
  117.         herbivore = factory->CreateHerbivore();  
  118.     }  
  119.   
  120.     // Methods  
  121.     void RunFoodChain()  
  122.     {   
  123.         carnivore->Eat(herbivore);   
  124.     }  
  125. };  
  126.   
  127.   
  128. int _tmain(int argc, _TCHAR* argv[])  
  129. {  
  130.     // Create and run the Africa animal world  
  131.     ContinentFactory *africa = new AfricaFactory();  
  132.     AnimalWorld *world = new AnimalWorld( africa );  
  133.     world->RunFoodChain();  
  134.   
  135.     // Create and run the America animal world  
  136.     ContinentFactory *america = new AmericaFactory();  
  137.     world = new AnimalWorld( america );  
  138.     world->RunFoodChain();  
  139.   
  140.     return 0;  
  141. }  

“开放-封闭”原则:


抽象工厂可以很好的应对增加新产品族的问题(即a4/b4/c4),且符合“开放-封闭”原则,但是若是增加新的产品结构的话(即d/d1/d2),就是说a/b/c/d这4中方法必须同时使用了,那就必须修改工厂角色。不符合“开放-封闭”原则。综合来讲,抽象工厂模式以一种倾斜的方式支持增加新的产品,它为新产品族的增加提供方便,而不能为新的产品结构的增加提供这样的方便。

实现要点:

  • 在抽象工厂模式中,选用哪种产品族的问题,需要采用工厂方法或简单工厂模式来配合解决。
  • 抽象工厂模式和工厂方法模式一样,都把对象的创建延迟到了他的子类中。
  • 具体的工厂类可以设计成单例类,他只向外界提供自己唯一的实例。

与其他工厂模式的联系和异同:

  • 抽象工厂模式中的具体工厂负责生产一个产品族的产品。而产品族的增加只需要增加与其对应的具体工厂。
  • 3种工厂模式都是创建型模式,都是创建对象的,但都把产品具体创建的过程给隐藏了。
  • 工厂方法模式是针对一种产品结构,而抽象工厂模式是针对多种产品结构。

适用性:

在以下情况下应当考虑使用抽象工厂模式:

  • 一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节,这对于所有形态的工厂模式都是重要的。
  • 这个系统有多于一个的产品族,而系统只消费其中某一产品族。
  • 同属于同一个产品族的产品是在一起使用的,这一约束必须在系统的设计中体现出来。
  • 系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于实现。

应用场景:

  • 支持多种观感标准的用户界面工具箱(Kit)。
  • 游戏开发中的多风格系列场景,比如道路,房屋,管道等。
0 0
原创粉丝点击