forked from gongjianbo/CppDesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6FactoryMethodPattern.h
More file actions
84 lines (79 loc) · 2.12 KB
/
Copy path6FactoryMethodPattern.h
File metadata and controls
84 lines (79 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Factory Method(工厂方法-对象创建型)
//别名Virtual Constructor(虚构造器)
//定义一个用于创建目标对象的接口,让子类决定实例化哪一个目标类。
//Factory Method 使一个类的实例化延迟到其子类。
#include <iostream>
class IProduct
{
public:
virtual ~IProduct() {}
};
class AProduct : public IProduct {};
class BProduct : public IProduct {};
//方式1:Creator/Factory缺省实现+参数化工厂方法
//(简单工厂)
class SimpleFactory
{
public:
virtual IProduct* create(int type) {
switch (type)
{
case 0: return new AProduct();
case 1: return new BProduct();
default: break;
}
return nullptr;
}
};
//方式2:子类化
class IFactory
{
public:
virtual ~IFactory() {}
virtual IProduct* create() = 0;
};
class AFactory : public IFactory
{
public:
IProduct* create() override {
return new AProduct();
}
};
class BFactory : public IFactory
{
public:
IProduct* create() override {
return new BProduct();
}
};
//测试
void testFactoryMethodPattern()
{
//简单工厂,根据参数来创建对应对象
std::cout << "eg.1" << std::endl;
std::cout << "construct SimpleFactory" << std::endl;
SimpleFactory* f = new SimpleFactory();
std::cout << "SimpleFactory create AProduct through flag" << std::endl;
IProduct* p1 = f->create(0);
std::cout << "AProduct Construction completed" << std::endl;
delete f;
delete p1;
//factory可作为参数传递给类实例,用户再调用factory的create函数
//如我们提供一个带factory参数的接口,后面使用的时候再根据需求创建具体的factory
std::cout << "eg.2" << std::endl;
std::cout << "construct AFactory" << std::endl;
IFactory* factory = new AFactory();
std::cout << "AFactory create AProduct" << std::endl;
IProduct* p2 = factory->create();
std::cout << "AProduct Construction completed" << std::endl;
delete factory;
delete p2;
//
std::cout << "construct BFactory" << std::endl;
factory = new BFactory();
std::cout << "BFactory create BProduct" << std::endl;
p2 = factory->create();
std::cout << "BProduct Construction completed" << std::endl;
delete factory;
delete p2;
}