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

工厂模式

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

标签:设计模式   实例化   www.   header   out   closed   类型   系统   fine   

工厂模式

  这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

介绍

意图:定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。

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

何时使用:我们明确地计划不同条件下创建不同实例时。

如何解决:让其子类实现工厂接口,返回的也是一个抽象的产品。

关键代码:创建过程在其子类执行。

优点: 1、一个调用者想创建一个对象,只要知道其名称就可以了。 2、扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以。 3、屏蔽产品的具体实现,调用者只关心产品的接口。

缺点:每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加,在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖。这并不是什么好事。

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

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

技术分享图片
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 #include "shape.h"
 6 #include "rectangle.h"
 7 #include "square.h"
 8 #include "circle.h"
 9 #include "shapeFactory.h"
10 
11 int main()
12 {
13     ShapeFactory *shapeFactory = new ShapeFactory;
14 
15     /**< 获取Circle的对象,并调用它的draw方法 */
16     Shape *shape1 = shapeFactory->getShape("CIRCLE");
17 
18     /**< 调用 Circle 的 draw 方法 */
19     shape1->draw();
20 
21 
22     /**< 获取 Rectangle 的对象,并调用它的 draw 方法 */
23 
24     Shape *shape2 = shapeFactory->getShape("RECTANGLE");
25 
26     /**< 调用 Rectangle 的 draw 方法 */
27     shape2->draw();
28 
29 
30     /**< 获取 Square 的对象,并调用它的 draw 方法 */
31     Shape *shape3 = shapeFactory->getShape("SQUARE");
32 
33     /**< 调用 Square 的 draw 方法 */
34     shape3->draw();
35 
36     delete shapeFactory;
37         delete shape1;
38             delete shape2;
39                 delete shape3;
40 
41     return 0;
42 }
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 #ifndef __CIRCLR_H__
 2 #define __CIRCLR_H__
 3 
 4 #include "shape.h"
 5 
 6 class Circle : public Shape
 7 {
 8     public:
 9         virtual void draw()
10         {
11             std::cout << "Inside Circle::draw() method." << endl;
12         }
13         ~Circle()
14         {
15 
16         }
17 };
18 
19 #endif // __CIRCLR_H__
circle.h
技术分享图片
 1 #ifndef __CIRCLR_H__
 2 #define __CIRCLR_H__
 3 
 4 #include "shape.h"
 5 
 6 class Circle : public Shape
 7 {
 8     public:
 9         virtual void draw()
10         {
11             std::cout << "Inside Circle::draw() method." << endl;
12         }
13         ~Circle()
14         {
15 
16         }
17 };
18 
19 #endif // __CIRCLR_H__
square.h
技术分享图片
 1 #ifndef __CIRCLR_H__
 2 #define __CIRCLR_H__
 3 
 4 #include "shape.h"
 5 
 6 class Circle : public Shape
 7 {
 8     public:
 9         virtual void draw()
10         {
11             std::cout << "Inside Circle::draw() method." << endl;
12         }
13         ~Circle()
14         {
15 
16         }
17 };
18 
19 #endif // __CIRCLR_H__
rectangle.h
技术分享图片
 1 #ifndef __SHAPE_FACTORY_H__
 2 #define __SHAPE_FACTORY_H__
 3 
 4 #include "shape.h"
 5 #include "rectangle.h"
 6 #include "square.h"
 7 #include "circle.h"
 8 
 9 class ShapeFactory
10 {
11     public:
12         Shape* getShape(const string &shapeType)
13         {
14             if(shapeType.empty())
15                 return nullptr;
16 
17             if(shapeType == "CIRCLE")
18             {
19                 return new Circle;
20             }else if(shapeType == "RECTANGLE")
21             {
22                 return new Rectangle;
23             }else if(shapeType == "SQUARE")
24             {
25                 return new Square;
26             }
27             return nullptr;
28         }
29 };
30 
31 #endif // __SHAPE_FACTORY_H__
shapeFactory.h

 注意:C++类有继承时,析构函数必须为虚函数。如果不是虚函数,则使用时可能存在内在泄漏的问题。

工厂模式

标签:设计模式   实例化   www.   header   out   closed   类型   系统   fine   

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

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