-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
235 lines (182 loc) · 4.44 KB
/
main.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
using namespace std;
class Ingredient1
{
public:
virtual void exec1() = 0;
};
class Ingredient1_Kind1 : public Ingredient1
{
public:
Ingredient1_Kind1()
{
cout << "Ingredient1_Kind1 constructor...\n";
}
void exec1()
{
cout << "Executing: Ingredient1_Kind1\n";
}
};
class Ingredient1_Kind2 : public Ingredient1
{
public:
Ingredient1_Kind2()
{
cout << "Ingredient1_Kind2 constructor...\n";
}
void exec1()
{
cout << "Executing: Ingredient1_Kind2\n";
}
};
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
class Ingredient2
{
public:
virtual void exec2() = 0;
};
class Ingredient2_Kind1 : public Ingredient2
{
public:
Ingredient2_Kind1()
{
cout << "Ingredient2_Kind1 constructor...\n";
}
void exec2()
{
cout << "Executing: Ingredient2_Kind1\n";
}
};
class Ingredient2_Kind2 : public Ingredient2
{
public:
Ingredient2_Kind2()
{
cout << "Ingredient2_Kind2 constructor...\n";
}
void exec2()
{
cout << "Executing: Ingredient2_Kind2\n";
}
};
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
class Ingredient3
{
public:
virtual void exec3() = 0;
};
class Ingredient3_Kind1 : public Ingredient3
{
public:
Ingredient3_Kind1()
{
cout << "Ingredient3_Kind1 constructor...\n";
}
void exec3()
{
cout << "Executing: Ingredient3_Kind1\n";
}
};
class Ingredient3_Kind2 : public Ingredient3
{
public:
Ingredient3_Kind2()
{
cout << "Ingredient3_Kind2 constructor...\n";
}
void exec3()
{
cout << "Executing: Ingredient3_Kind2\n";
}
};
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
class Final_Product
{
public:
Ingredient1* ingredient1;
Ingredient2* ingredient2;
Ingredient3* ingredient3;
void show_final_product()
{
ingredient1->exec1();
ingredient2->exec2();
ingredient3->exec3();
}
};
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
class Builder
{
public:
virtual Ingredient1* new_Ingredient1() = 0;
virtual Ingredient2* new_Ingredient2() = 0;
virtual Ingredient3* new_Ingredient3() = 0;
};
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
class Product1 : public Builder
{
public:
Ingredient1* new_Ingredient1()
{
Ingredient1* ingredient1 = new Ingredient1_Kind1;
return ingredient1;
}
Ingredient2* new_Ingredient2()
{
Ingredient2* ingredient2 = new Ingredient2_Kind1;
return ingredient2;
}
Ingredient3* new_Ingredient3()
{
Ingredient3* ingredient3 = new Ingredient3_Kind1;
return ingredient3;
}
};
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
class Director
{
public:
Builder* builder;
void new_Builder( Builder* b )
{
builder = b;
}
Final_Product* new_FinalProduct()
{
Final_Product* final_product = new Final_Product;
final_product->ingredient1 = builder->new_Ingredient1();
final_product->ingredient2 = builder->new_Ingredient2();
final_product->ingredient3 = builder->new_Ingredient3();
return final_product;
}
};
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
int main()
{
cout << "\nA final product built with director and ingredients of one kind:\n";
Final_Product* final_product1;
Director director;
Product1 product1_Builder;
director.new_Builder( &product1_Builder );
final_product1 = director.new_FinalProduct();
final_product1->show_final_product();
cout << "\nA final product built without director and ingredients of different kind:\n";
Final_Product final_product2;
final_product2.ingredient1 = new Ingredient1_Kind2;
final_product2.ingredient2 = new Ingredient2_Kind2;
final_product2.ingredient3 = new Ingredient3_Kind2;
final_product2.show_final_product();
}