(二)工厂方法模式-C++实现

工厂方法模式:定义一个用于创建对象的借口,让子类决定实例化哪一个类。 Factory method使一个类的实例化延迟到子类。

    当系统准备为用户提供某个类的子类的实例,又不想让用户代码和孩子类形成耦合时,可以使用工厂方法模式来设计系统。

工厂方法模式的关键在于工厂接口里面有一个抽象方法,在具体工厂中,这个方法返回某个产品的实例。

    工厂方法模式中有四个角色:

1、构造者(Factory)

2、具体构造者(Concrete Factory)

3、抽象产品(Product)

4、具体产品(Concrete Product)

    在实际的依赖关系中,构造者(工厂)会依赖产品。

    本文C++实现的例子中,圆珠笔是用户需要的,而笔芯是具体产品,所以定义了PenCore作为抽象产品,构造者是生产圆珠笔的工厂BallPen,因为有三种具体产品,所以相应的也有三种具体构造者。

总共有5个文件,两个头文件,两个源文件,一个主函数作为测试文件。

1、Factory.h

 1 #ifndef _FACTORY_H_
 2 #define _FACTORY_H_
 3 #include <iostream>
 4 #include <string>
 5 #include "Product.h"
 6 using namespace std;
 7 
 8 class PenCore;
 9 
10 //抽象工厂-这里是钢笔类
11 class BallPen
12 {
13 public:
14     BallPen(){};
15 
16     virtual ~BallPen(){}
17 
18     virtual PenCore* getPenCore() = 0;
19 
20 
21 };
22 
23 //具体工厂
24 class RedBallPen : public BallPen
25 {
26 public:
27     PenCore* getPenCore();
28 };
29 
30 class BlueBallPen : public BallPen
31 {
32 public:
33     PenCore* getPenCore();
34 };
35 
36 class BlackBallPen : public BallPen
37 {
38 public:
39     PenCore* getPenCore();
40 };
41 
42 
43 
44 #endif
View Code

2、Factory.cpp

 1 #include "Factory.h"
 2 
 3 PenCore* RedBallPen::getPenCore()
 4 {
 5     PenCore *pen = new RedPenCore();
 6     return pen;
 7 }
 8 
 9 PenCore* BlueBallPen::getPenCore()
10 {
11     PenCore *pen = new BluePenCore();
12     return pen;
13 }
14 
15 PenCore* BlackBallPen::getPenCore()
16 {
17     PenCore *pen = new BlackPenCore();
18     return pen;
19 }
View Code

3、Product.h

 1 #ifndef _PRODUCT_H_
 2 #define _PRODUCT_H_
 3 #include <iostream>
 4 #include <string>
 5 using namespace std;
 6 
 7 //抽象产品类
 8 class PenCore
 9 {
10 public:
11     PenCore(){}
12 
13     virtual ~PenCore(){}
14 
15     virtual void writeWord(string s) = 0;
16 
17 public:
18     string color;
19 };
20 
21 //具体产品类
22 class RedPenCore : public PenCore
23 {
24 public:
25     RedPenCore();
26 
27     void writeWord(string s);
28 };
29 
30 class BluePenCore : public PenCore
31 {
32 public:
33     BluePenCore();
34 
35     void writeWord(string s);
36 
37 };
38 
39 class BlackPenCore : public PenCore
40 {
41 public:
42     BlackPenCore();
43 
44     void writeWord(string s);
45 
46 };
47 #endif
View Code

4、Product.cpp

 1 #include "Product.h"
 2 
 3 RedPenCore::RedPenCore()
 4 {
 5     color = "Red";
 6 }
 7 
 8 void RedPenCore::writeWord(string s)
 9 {
10     cout << "write " << color << "  " << s << endl;
11 }
12 
13 BluePenCore::BluePenCore()
14 {
15     color = "Blue";
16 }
17 
18 void BluePenCore::writeWord(string s)
19 {
20     cout << "write " << color << "  " << s << endl;
21 }
22 
23 BlackPenCore::BlackPenCore()
24 {
25     color = "Black";
26 }
27 
28 void BlackPenCore::writeWord(string s)
29 {
30     cout << "write " << color << "  " << s << endl;
31 }
View Code

5、VirtualConstructorTest.cpp

 1 #include "Factory.h"
 2 #include "Product.h"
 3 
 4 int main()
 5 {
 6     BallPen *ballPen = new BlueBallPen();
 7     PenCore *pen = ballPen->getPenCore();
 8     pen->writeWord("hello");
 9     delete pen;
10     pen = NULL;
11     delete ballPen;
12     ballPen = NULL;
13 
14     ballPen = new RedBallPen();
15     pen = ballPen->getPenCore();
16     pen->writeWord("world");
17     delete ballPen;
18     ballPen = NULL;
19     delete pen;
20     pen = NULL;
21 
22     return 0;
23 }
View Code

    C++中实现要注意的是,首先工厂中的抽象方法返回类型是抽象产品的指针,然后在C++中自己new的对象一定要delete,防止内存泄露。

与java不同的是,在C++中,抽象工厂中的构造函数并不能调用自己另外的纯虚函数,因为这个函数需要子类实现之后才能调用,而在java中确可以。

原文地址:https://www.cnblogs.com/leewhite/p/6033084.html