码迷,mamicode.com
首页 > 其他好文 > 详细

抽象工厂模式

时间:2018-09-02 02:01:02      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:abs   调用   分享图片   fine   sign   aaa   ptr   创建型   opened   

抽象工厂模式

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

介绍

意图:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

主要解决:主要解决接口选择的问题。

何时使用:系统的产品有多于一个的产品族,而系统只消费其中某一族的产品。

如何解决:在一个产品族里面,定义多个产品。

关键代码:在一个工厂里聚合多个同类产品。

优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。

缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。

具体内容参考:http://www.runoob.com/design-pattern/abstract-factory-pattern.html

下面是我实现的C++版代码:

技术分享图片
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 #include "FactoryProducer.h"
 6 
 7 int main()
 8 {
 9       //获取形状工厂
10       AbstractFactory *shapeFactory = FactoryProducer::getFactory("SHAPE");
11 
12       //获取形状为 Circle 的对象
13       Shape *shape1 = shapeFactory->getShape("CIRCLE");
14 
15       //调用 Circle 的 draw 方法
16       shape1->draw();
17 
18       //获取形状为 Rectangle 的对象
19       Shape *shape2 = shapeFactory->getShape("RECTANGLE");
20 
21       //调用 Rectangle 的 draw 方法
22       shape2->draw();
23 
24       //获取形状为 Square 的对象
25       Shape *shape3 = shapeFactory->getShape("SQUARE");
26 
27       //调用 Square 的 draw 方法
28       shape3->draw();
29 
30       //获取颜色工厂
31       AbstractFactory *colorFactory = FactoryProducer::getFactory("COLOR");
32 
33       //获取颜色为 Red 的对象
34       Color *color1 = colorFactory->getColor("RED");
35 
36       //调用 Red 的 fill 方法
37       color1->fill();
38 
39       //获取颜色为 Green 的对象
40       Color *color2 = colorFactory->getColor("GREEN");
41 
42       //调用 Green 的 fill 方法
43       color2->fill();
44 
45       //获取颜色为 Blue 的对象
46       Color *color3 = colorFactory->getColor("BLUE");
47 
48       //调用 Blue 的 fill 方法
49       color3->fill();
50 
51     return 0;
52 }
main.c
技术分享图片
 1 #ifndef __SHAPE_H__
 2 #define __SHAPE_H__
 3 
 4 class Shape
 5 {
 6     public:
 7         virtual void draw()=0;
 8         virtual ~Shape(){};
 9 };
10 
11 #endif // __SHAPE_H__
shape.h
技术分享图片
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 #include "FactoryProducer.h"
 6 
 7 int main()
 8 {
 9       //获取形状工厂
10       AbstractFactory *shapeFactory = FactoryProducer::getFactory("SHAPE");
11 
12       //获取形状为 Circle 的对象
13       Shape *shape1 = shapeFactory->getShape("CIRCLE");
14 
15       //调用 Circle 的 draw 方法
16       shape1->draw();
17 
18       //获取形状为 Rectangle 的对象
19       Shape *shape2 = shapeFactory->getShape("RECTANGLE");
20 
21       //调用 Rectangle 的 draw 方法
22       shape2->draw();
23 
24       //获取形状为 Square 的对象
25       Shape *shape3 = shapeFactory->getShape("SQUARE");
26 
27       //调用 Square 的 draw 方法
28       shape3->draw();
29 
30       //获取颜色工厂
31       AbstractFactory *colorFactory = FactoryProducer::getFactory("COLOR");
32 
33       //获取颜色为 Red 的对象
34       Color *color1 = colorFactory->getColor("RED");
35 
36       //调用 Red 的 fill 方法
37       color1->fill();
38 
39       //获取颜色为 Green 的对象
40       Color *color2 = colorFactory->getColor("GREEN");
41 
42       //调用 Green 的 fill 方法
43       color2->fill();
44 
45       //获取颜色为 Blue 的对象
46       Color *color3 = colorFactory->getColor("BLUE");
47 
48       //调用 Blue 的 fill 方法
49       color3->fill();
50 
51     return 0;
52 }
rectangle.h
技术分享图片
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 #include "FactoryProducer.h"
 6 
 7 int main()
 8 {
 9       //获取形状工厂
10       AbstractFactory *shapeFactory = FactoryProducer::getFactory("SHAPE");
11 
12       //获取形状为 Circle 的对象
13       Shape *shape1 = shapeFactory->getShape("CIRCLE");
14 
15       //调用 Circle 的 draw 方法
16       shape1->draw();
17 
18       //获取形状为 Rectangle 的对象
19       Shape *shape2 = shapeFactory->getShape("RECTANGLE");
20 
21       //调用 Rectangle 的 draw 方法
22       shape2->draw();
23 
24       //获取形状为 Square 的对象
25       Shape *shape3 = shapeFactory->getShape("SQUARE");
26 
27       //调用 Square 的 draw 方法
28       shape3->draw();
29 
30       //获取颜色工厂
31       AbstractFactory *colorFactory = FactoryProducer::getFactory("COLOR");
32 
33       //获取颜色为 Red 的对象
34       Color *color1 = colorFactory->getColor("RED");
35 
36       //调用 Red 的 fill 方法
37       color1->fill();
38 
39       //获取颜色为 Green 的对象
40       Color *color2 = colorFactory->getColor("GREEN");
41 
42       //调用 Green 的 fill 方法
43       color2->fill();
44 
45       //获取颜色为 Blue 的对象
46       Color *color3 = colorFactory->getColor("BLUE");
47 
48       //调用 Blue 的 fill 方法
49       color3->fill();
50 
51     return 0;
52 }
square.h
技术分享图片
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 #include "FactoryProducer.h"
 6 
 7 int main()
 8 {
 9       //获取形状工厂
10       AbstractFactory *shapeFactory = FactoryProducer::getFactory("SHAPE");
11 
12       //获取形状为 Circle 的对象
13       Shape *shape1 = shapeFactory->getShape("CIRCLE");
14 
15       //调用 Circle 的 draw 方法
16       shape1->draw();
17 
18       //获取形状为 Rectangle 的对象
19       Shape *shape2 = shapeFactory->getShape("RECTANGLE");
20 
21       //调用 Rectangle 的 draw 方法
22       shape2->draw();
23 
24       //获取形状为 Square 的对象
25       Shape *shape3 = shapeFactory->getShape("SQUARE");
26 
27       //调用 Square 的 draw 方法
28       shape3->draw();
29 
30       //获取颜色工厂
31       AbstractFactory *colorFactory = FactoryProducer::getFactory("COLOR");
32 
33       //获取颜色为 Red 的对象
34       Color *color1 = colorFactory->getColor("RED");
35 
36       //调用 Red 的 fill 方法
37       color1->fill();
38 
39       //获取颜色为 Green 的对象
40       Color *color2 = colorFactory->getColor("GREEN");
41 
42       //调用 Green 的 fill 方法
43       color2->fill();
44 
45       //获取颜色为 Blue 的对象
46       Color *color3 = colorFactory->getColor("BLUE");
47 
48       //调用 Blue 的 fill 方法
49       color3->fill();
50 
51     return 0;
52 }
circle.h
技术分享图片
 1 #ifndef __COLOR_H__
 2 #define __COLOR_H__
 3 
 4 class Color
 5 {
 6     public:
 7         virtual void fill() = 0;
 8         virtual ~Color(){};
 9 };
10 
11 #endif // __COLOR_H__
color.h
技术分享图片
 1 #ifndef __RED_H__
 2 #define __RED_H__
 3 
 4 #include "color.h"
 5 
 6 class Red : public Color
 7 {
 8     public:
 9         virtual void fill()
10         {
11             std::cout << "Inside Red::fill() method." << std::endl;
12         }
13         ~Red()
14         {
15         }
16 };
17 
18 #endif // __RED_H__
red.h
技术分享图片
 1 #ifndef __GREEN_H__
 2 #define __GREEN_H__
 3 
 4 #include "color.h"
 5 
 6 class Green : public Color
 7 {
 8     public:
 9         virtual void fill()
10         {
11             std::cout << "Inside Green::fill() method." << std::endl;
12         }
13         ~Green()
14         {
15         }
16 };
17 
18 #endif // __GREEN_H__
green.h
技术分享图片
 1 #ifndef __BLUE_H__
 2 #define __BLUE_H__
 3 
 4 #include "color.h"
 5 
 6 class Blue : public Color
 7 {
 8     public:
 9         virtual void fill()
10         {
11             std::cout << "Inside Blue::fill() method." << std::endl;
12         }
13         ~Blue()
14         {
15         }
16 };
17 
18 #endif // __BLUE_H__
blue.h
技术分享图片
 1 #ifndef __ABSTRACT_FACTORY_H__
 2 #define __ABSTRACT_FACTORY_H__
 3 
 4 #include "string"
 5 
 6 #include "color.h"
 7 #include "shape.h"
 8 
 9 class AbstractFactory
10 {
11     public:
12         virtual Shape* getShape(const string &shape)=0;
13         virtual Color* getColor(const string &color)=0;
14 
15 
16 
17         //virtual ~AbstractFactory(){};
18 
19 };
20 
21 #endif // __ABSTRACT_FACTORY_H__
abstractFactory.h
技术分享图片
 1 #ifndef __SHAPE_FACTORY_H__
 2 #define __SHAPE_FACTORY_H__
 3 
 4 #include <string>
 5 
 6 #include "rectangle.h"
 7 #include "square.h"
 8 #include "circle.h"
 9 
10 #include "abstractFactory.h"
11 
12 class ShapeFactory : virtual public AbstractFactory
13 {
14     public:
15         virtual Shape* getShape(const string &shapeType)
16         {
17             if(shapeType.empty())
18                 return nullptr;
19 
20             if(shapeType == "CIRCLE")
21             {
22                 return new Circle;
23             }else if(shapeType == "RECTANGLE")
24             {
25                 return new Rectangle;
26             }else if(shapeType == "SQUARE")
27             {
28                 return new Square;
29             }
30             return nullptr;
31         }
32 
33         virtual Color* getColor(const string &color)
34         {
35             return nullptr;
36         }
37 
38 };
39 
40 #endif // __SHAPE_FACTORY_H__
shapeFactory.h
技术分享图片
 1 #ifndef __COLOR_FACTORY_H__
 2 #define __COLOR_FACTORY_H__
 3 
 4 #include <string>
 5 
 6 #include "red.h"
 7 #include "green.h"
 8 #include "blue.h"
 9 
10 #include "abstractFactory.h"
11 
12 class ColorFactory : virtual public AbstractFactory
13 {
14 public:
15     virtual Shape* getShape(const string &shapeType)
16     {
17         return nullptr;
18     }
19 
20     virtual Color* getColor(const string &color)
21     {
22         if(color.empty())
23             return nullptr;
24 
25         if(color == "RED")
26         {
27             return new Red;
28         }else if(color == "GREEN")
29         {
30             return new Green;
31         }else if(color == "BLUE")
32         {
33             return new Blue;
34         }
35         return nullptr;
36     }
37 };
38 
39 #endif // __COLOR_FACTORY_H__
colorFactory.h
技术分享图片
 1 #ifndef __FACTORY_PRODUCER_H__
 2 #define __FACTORY_PRODUCER_H__
 3 
 4 #include "shapeFactory.h"
 5 #include "colorFactory.h"
 6 
 7 class FactoryProducer
 8 {
 9 public:
10     static AbstractFactory* getFactory(string choice)
11     {
12         if(choice == "SHAPE")
13         {
14             return new ShapeFactory;
15         }else if(choice == "COLOR")
16         {
17             return new ColorFactory;
18         }
19 
20         return nullptr;
21     }
22 };
23 
24 #endif // __FACTORY_PRODUCER_H__
FactoryProducer.h

 

抽象工厂模式

标签:abs   调用   分享图片   fine   sign   aaa   ptr   创建型   opened   

原文地址:https://www.cnblogs.com/fenghualong/p/9572098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!