forked from munificent/game-programming-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype-object.h
More file actions
304 lines (254 loc) · 4.55 KB
/
Copy pathtype-object.h
File metadata and controls
304 lines (254 loc) · 4.55 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
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
294
295
296
297
298
299
300
301
302
303
304
#include "common.h"
namespace Subclasses
{
//^1
class Monster
{
public:
virtual ~Monster() {}
virtual const char* getAttack() = 0;
protected:
Monster(int startingHealth)
: health_(startingHealth)
{}
private:
int health_; // Current health.
};
//^1
//^2
class Dragon : public Monster
{
public:
Dragon() : Monster(230) {}
virtual const char* getAttack()
{
return "The dragon breathes fire!";
}
};
class Troll : public Monster
{
public:
Troll() : Monster(48) {}
virtual const char* getAttack()
{
return "The troll clubs you!";
}
};
//^2
}
namespace NoInheritance
{
//^3
class Breed
{
public:
Breed(int health, const char* attack)
: health_(health),
attack_(attack)
{}
int getHealth() { return health_; }
const char* getAttack() { return attack_; }
private:
int health_; // Starting health.
const char* attack_;
};
//^3
//^4
class Monster
{
public:
Monster(Breed& breed)
: health_(breed.getHealth()),
breed_(breed)
{}
const char* getAttack()
{
return breed_.getAttack();
}
private:
int health_; // Current health.
Breed& breed_;
};
//^4
void testUsage()
{
Breed& someBreed = *(new Breed(123, "attack"));
//^7
Monster* monster = new Monster(someBreed);
//^7
use(monster);
}
}
namespace BreedCtor
{
class Breed;
class Monster
{
public:
Monster(Breed& breed) {}
};
//^5
class Breed
{
public:
Monster* newMonster() { return new Monster(*this); }
// Previous Breed code...
//^omit
Breed(int health, const char* attack)
: health_(health),
attack_(attack)
{}
int getHealth() { return health_; }
const char* getAttack() { return attack_; }
private:
int health_; // Starting health.
const char* attack_;
//^omit
};
//^5
void testUsage()
{
Breed& someBreed = *(new Breed(1, "foo"));
//^8
Monster* monster = someBreed.newMonster();
//^8
use(monster);
}
}
namespace BreedCtorMonster
{
class Breed
{
public:
int getHealth() { return 0; }
const char* getAttack() { return "s"; }
};
//^6
class Monster
{
friend class Breed;
public:
const char* getAttack() { return breed_.getAttack(); }
private:
Monster(Breed& breed)
: health_(breed.getHealth()),
breed_(breed)
{}
int health_; // Current health.
Breed& breed_;
};
//^6
}
namespace Inheritance
{
//^9
class Breed
{
public:
Breed(Breed* parent, int health, const char* attack)
: parent_(parent),
health_(health),
attack_(attack)
{}
int getHealth();
const char* getAttack();
private:
Breed* parent_;
int health_; // Starting health.
const char* attack_;
};
//^9
//^10
int Breed::getHealth()
{
// Override.
if (health_ != 0 || parent_ == NULL) return health_;
// Inherit.
return parent_->getHealth();
}
const char* Breed::getAttack()
{
// Override.
if (attack_ != NULL || parent_ == NULL) return attack_;
// Inherit.
return parent_->getAttack();
}
//^10
}
namespace CopyDown
{
class Breed
{
public:
//^copy-down
Breed(Breed* parent, int health, const char* attack)
: health_(health),
attack_(attack)
{
// Inherit non-overridden attributes.
if (parent != NULL)
{
if (health == 0) health_ = parent->getHealth();
if (attack == NULL) attack_ = parent->getAttack();
}
}
//^copy-down
//^copy-down-access
int getHealth() { return health_; }
const char* getAttack() { return attack_; }
//^copy-down-access
private:
int health_; // Starting health.
const char* attack_;
};
}
namespace ExposeBreed
{
class Breed;
//^11
class Monster
{
public:
Breed& getBreed() { return breed_; }
// Existing code...
//^omit
Breed& breed_;
//^omit
};
//^11
}
namespace OverrideAttack
{
class Breed
{
public:
Breed(const char* attack)
: attack_(attack)
{}
const char* getAttack() { return attack_; }
private:
const char* attack_;
};
class Monster
{
public:
Monster(Breed& breed)
: breed_(breed)
{}
const char* getAttack();
private:
int health_;
Breed& breed_;
};
#define LOW_HEALTH 1
//^12
const char* Monster::getAttack()
{
if (health_ < LOW_HEALTH)
{
return "The monster flails weakly.";
}
return breed_.getAttack();
}
//^12
}