-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract factory.cpp
More file actions
141 lines (129 loc) · 2.49 KB
/
abstract factory.cpp
File metadata and controls
141 lines (129 loc) · 2.49 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类
#include <iostream>
using namespace std;
// Product A
class ProductA
{
public:
virtual void Show() = 0;
};
class ProductA1 : public ProductA
{
public:
void Show()
{
cout << "I'm ProductA1" << endl;
}
};
class ProductA2 : public ProductA
{
public:
void Show()
{
cout << "I'm ProductA2" << endl;
}
};
// Product B
class ProductB
{
public:
virtual void Show() = 0;
};
class ProductB1 : public ProductB
{
public:
void Show()
{
cout << "I'm ProductB1" << endl;
}
};
class ProductB2 : public ProductB
{
public:
void Show()
{
cout << "I'm ProductB2" << endl;
}
};
// Factory
class Factory
{
public:
virtual ProductA* CreateProductA() = 0;
virtual ProductB* CreateProductB() = 0;
};
class Factory1 : public Factory
{
public:
ProductA* CreateProductA()
{
return new ProductA1();
}
ProductB* CreateProductB()
{
return new ProductB1();
}
};
class Factory2 : public Factory
{
ProductA* CreateProductA()
{
return new ProductA2();
}
ProductB* CreateProductB()
{
return new ProductB2();
}
};
//int main(int argc, char* argv[])
//{
// Factory* factoryObj1 = new Factory1();
// ProductA* productObjA1 = factoryObj1->CreateProductA();
// ProductB* productObjB1 = factoryObj1->CreateProductB();
//
// productObjA1->Show();
// productObjB1->Show();
//
// Factory* factoryObj2 = new Factory2();
// ProductA* productObjA2 = factoryObj2->CreateProductA();
// ProductB* productObjB2 = factoryObj2->CreateProductB();
//
// productObjA2->Show();
// productObjB2->Show();
//
// if (factoryObj1 != NULL)
// {
// delete factoryObj1;
// factoryObj1 = NULL;
// }
//
// if (productObjA1 != NULL)
// {
// delete productObjA1;
// productObjA1 = NULL;
// }
//
// if (productObjB1 != NULL)
// {
// delete productObjB1;
// productObjB1 = NULL;
// }
//
// if (factoryObj2 != NULL)
// {
// delete factoryObj2;
// factoryObj2 = NULL;
// }
//
// if (productObjA2 != NULL)
// {
// delete productObjA2;
// productObjA2 = NULL;
// }
//
// if (productObjB2 != NULL)
// {
// delete productObjB2;
// productObjB2 = NULL;
// }
//}