/
builder.cpp
executable file
·294 lines (211 loc) · 5.24 KB
/
builder.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Builder pattern:
1. it let you vary a product's internal representation, the builder object provide the director with an abstract interface for constructing the product
the interface let the builder hide the representation and internal structure of product, it also hide how the product get assembled, because the product
is constructed through an abstract interface. all you have to do to change product's internal representation is define a new kind of builder
2. it isolates code for construction and representation . the Builder pattern improves modularity by encapsulating the way a complex object is constructed
and repsented, client needn't know anything about the classes the define product's internal structure, such classes not appear in builder's interface
Each concreteBuilder contains all the code to create and assemble a particular kind of product.
3.it give you finer control over the construction process. unlike creational pattern that construct product in one shot, the builder pattern constructs
the product step by step under the director's control, only when the product is finished does the director retrieve it from builder, hence the builder interface
reflects the process of constructing the product more than other creational patterns, this give you finer control over the construction process and consequently
the internal structure of the resulting product
*/
#include<iostream>
#include<map>
using namespace std;
enum Direction { North, South, East, West};
class MapSite
{
public:
virtual void Enter() = 0;
};
class Room : public MapSite
{
public:
Room(int roomNo)
{
_roomNumber = roomNo;
for(int i = 0 ; i < 4 ; i++)
{
_sides[i] = NULL;
}
}
MapSite* GetSide(Direction dir) const
{
return _sides[dir];
}
void SetSide(Direction dir, MapSite* site)
{
_sides[dir] = site;
}
virtual void Enter()
{
cout << "enter room" << endl;
}
public:
MapSite* _sides[4];
int _roomNumber;
};
class Wall : public MapSite
{
public:
Wall()
{
}
virtual void Enter()
{
cout << "this is wall , can't enter" << endl;
}
};
class Door : public MapSite
{
public:
Door(Room* roomA = 0 , Room* roomB = 0)
{
_room1 = roomA;
_room2 = roomB;
}
virtual void Enter()
{
cout << "enter the door" << endl;
}
Room* otherSideFrom(Room* aRoom)
{
if(aRoom == _room1)
{
return _room2;
}
else
{
return _room1;
}
}
private:
Room* _room1;
Room* _room2;
bool _isOpen;
};
class Maze
{
public:
Maze()
{
}
void AddRoom(Room* room)
{
roomMap[room->_roomNumber] = room;
}
Room* RoomNo(int roomNo)
{
return roomMap[roomNo];
}
int RoomSize()
{
return roomMap.size();
}
private:
map<int, Room*> roomMap;
};
class MazeBuilder
{
public:
virtual void BuildMaze() {}
virtual void BuildRoom(int room) {}
virtual void BuildDoor(int roomFrom , int roomTo) {}
virtual Maze* GetMaze() { return NULL;}
protected:
MazeBuilder() {}
};
class StandardMazeBuilder : public MazeBuilder
{
public:
StandardMazeBuilder()
{
_currentMaze = 0;
}
virtual void BuildMaze()
{
_currentMaze = new Maze;
}
virtual Maze* GetMaze()
{
return _currentMaze;
}
virtual void BuildRoom(int room)
{
if(_currentMaze != NULL && (_currentMaze->RoomNo(room) == NULL))
{
Room* roomA = new Room(room);
_currentMaze->AddRoom(roomA);
roomA->SetSide(North,new Wall);
roomA->SetSide(South,new Wall);
roomA->SetSide(East,new Wall);
roomA->SetSide(West,new Wall);
}
}
virtual void BuildDoor(int n1, int n2)
{
Room* r1 = _currentMaze->RoomNo(n1);
Room* r2 = _currentMaze->RoomNo(n2);
Door* d = new Door(r1,r2);
r1->SetSide(South,d);
r2->SetSide(North,d);
}
private:
Maze* _currentMaze;
};
class CountingMazeBuilder : public MazeBuilder
{
public:
CountingMazeBuilder()
{
_doors = 0;
_rooms = 0;
}
virtual void BuildMaze()
{
}
virtual void BuildRoom(int n)
{
_rooms++;
}
virtual void BuildDoor(int n1, int n2)
{
_doors++;
}
void GetCounts(int& rooms, int& doors)
{
rooms = _rooms;
doors = _doors;
}
private:
int _doors;
int _rooms;
};
class MazeGame
{
public:
Maze* CreateMaze(MazeBuilder& builder)
{
builder.BuildMaze();
builder.BuildRoom(1);
builder.BuildRoom(2);
builder.BuildDoor(1,2);
return builder.GetMaze();
}
};
int main(int argc, char** argv)
{
Maze* maze;
MazeGame game;
StandardMazeBuilder builder;
CountingMazeBuilder countBuilder;
game.CreateMaze(builder);
maze = builder.GetMaze();
game.CreateMaze(countBuilder);
cout << "room size in maze : " << maze->RoomSize() << endl;
int roomNum, doorNum;
countBuilder.GetCounts(roomNum,doorNum);
cout << "room Num : " << roomNum << " door Num: " << doorNum << endl;
}