-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory method.cpp
109 lines (95 loc) · 1.91 KB
/
factory method.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中
//https://www.cnblogs.com/wzxNote/p/13354686.html
#include <iostream>
class ISpliter
{
public:
virtual void split() = 0;
virtual ~ISpliter() {}
};
class SpliterFactory
{
public:
virtual ISpliter* CreateSpliter() = 0;
virtual ~SpliterFactory() {}
};
class BinarySpliter : public ISpliter
{
public:
void split()
{
std::cout << "BinarySpliter" << std::endl;
}
};
class TxtSpliter : public ISpliter
{
public:
void split()
{
std::cout << "TxtSpliter" << std::endl;
}
};
class PictureSpliter : public ISpliter
{
public:
void split()
{
std::cout << "TxtSpliter" << std::endl;
}
};
//具体工厂
class BinarySpliterFactory : public SpliterFactory
{
public:
virtual ISpliter* CreateSpliter()
{
std::cout << "BinarySpliterFactory" << std::endl;
return new BinarySpliter();
}
};
class TxtSpliterFactory : public SpliterFactory
{
public:
virtual ISpliter* CreateSpliter()
{
std::cout << "TxtSpliterFactory" << std::endl;
return new TxtSpliter();
}
};
class PictureSpliterFactory : public SpliterFactory
{
public:
virtual ISpliter* CreateSpliter()
{
std::cout << "PictureSpliterFactory" << std::endl;
return new PictureSpliter();
}
};
//int main() {
//
// std::cout << "工厂方法模式" << std::endl;
// //定义工厂类对象和产品类对象
// ISpliter* product = nullptr;
// SpliterFactory* factory = nullptr;
//
// factory = new BinarySpliterFactory();
// product = factory->CreateSpliter();
// product->split();
// delete factory;
// delete product;
//
//
// factory = new TxtSpliterFactory();
// product = factory->CreateSpliter();
// product->split();
// delete factory;
// delete product;
//
// factory = new PictureSpliterFactory();
// product = factory->CreateSpliter();
// product->split();
// delete factory;
// delete product;
//
// return 0;
//}