-
Notifications
You must be signed in to change notification settings - Fork 515
Expand file tree
/
Copy pathsingleton.h
More file actions
353 lines (297 loc) · 5.53 KB
/
Copy pathsingleton.h
File metadata and controls
353 lines (297 loc) · 5.53 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <iostream>
namespace Singleton1
{
//^1
class FileSystem
{
public:
static FileSystem& instance()
{
// Lazy initialize.
if (instance_ == NULL) instance_ = new FileSystem();
return *instance_;
}
private:
FileSystem() {}
static FileSystem* instance_;
};
//^1
FileSystem* FileSystem::instance_;
void test()
{
if (&FileSystem::instance() == NULL)
{
std::cout << "singleton is null!" << std::endl;
}
else
{
std::cout << "singleton is ok" << std::endl;
}
}
}
namespace SingletonStatic
{
//^local-static
class FileSystem
{
public:
static FileSystem& instance()
{
static FileSystem *instance = new FileSystem();
return *instance;
}
private:
FileSystem() {}
};
//^local-static
void test()
{
if (&FileSystem::instance() == NULL)
{
std::cout << "singleton is null!" << std::endl;
}
else
{
std::cout << "singleton is ok" << std::endl;
}
}
}
namespace Singleton2
{
//^2
class FileSystem
{
public:
virtual ~FileSystem() {}
virtual char* readFile(char* path) = 0;
virtual void writeFile(char* path, char* contents) = 0;
};
//^2
//^derived-file-systems
class PS3FileSystem : public FileSystem
{
public:
virtual char* readFile(char* path)
{
// Use Sony file IO API...
//^omit
return NULL;
//^omit
}
virtual void writeFile(char* path, char* contents)
{
// Use sony file IO API...
}
};
class WiiFileSystem : public FileSystem
{
public:
virtual char* readFile(char* path)
{
// Use Nintendo file IO API...
//^omit
return NULL;
//^omit
}
virtual void writeFile(char* path, char* contents)
{
// Use Nintendo file IO API...
}
};
//^derived-file-systems
}
namespace Singleton3
{
#define PLAYSTATION3 1
#define WII 2
#define PLATFORM PLAYSTATION3
class PS3FileSystem;
class WiiFileSystem;
//^3
class FileSystem
{
public:
static FileSystem& instance();
virtual ~FileSystem() {}
virtual char* readFile(char* path) = 0;
virtual void writeFile(char* path, char* contents) = 0;
protected:
FileSystem() {}
};
//^3
class PS3FileSystem : public FileSystem
{
virtual char* readFile(char* path)
{
return NULL;
}
virtual void writeFile(char* path, char* contents) {}
};
class WiiFileSystem : public FileSystem
{
virtual char* readFile(char* path)
{
return NULL;
}
virtual void writeFile(char* path, char* contents) {}
};
//^4
FileSystem& FileSystem::instance()
{
#if PLATFORM == PLAYSTATION3
static FileSystem *instance = new PS3FileSystem();
#elif PLATFORM == WII
static FileSystem *instance = new WiiFileSystem();
#endif
return *instance;
}
//^4
}
namespace Singleton4
{
//^5
class FileSystem
{
public:
static FileSystem& instance() { return instance_; }
private:
FileSystem() {}
static FileSystem instance_;
};
//^5
FileSystem FileSystem::instance_ = FileSystem();
}
namespace Singleton5
{
//^6
class FileSystem
{
public:
FileSystem()
{
assert(!instantiated_);
instantiated_ = true;
}
~FileSystem() { instantiated_ = false; }
private:
static bool instantiated_;
};
bool FileSystem::instantiated_ = false;
//^6
}
namespace Singleton7
{
#define SCREEN_WIDTH 100
#define SCREEN_HEIGHT 100
//^8
class Bullet
{
public:
int getX() const { return x_; }
int getY() const { return y_; }
void setX(int x) { x_ = x; }
void setY(int y) { y_ = y; }
private:
int x_, y_;
};
class BulletManager
{
public:
Bullet* create(int x, int y)
{
Bullet* bullet = new Bullet();
bullet->setX(x);
bullet->setY(y);
return bullet;
}
bool isOnScreen(Bullet& bullet)
{
return bullet.getX() >= 0 &&
bullet.getX() < SCREEN_WIDTH &&
bullet.getY() >= 0 &&
bullet.getY() < SCREEN_HEIGHT;
}
void move(Bullet& bullet)
{
bullet.setX(bullet.getX() + 5);
}
};
//^8
}
namespace Singleton8
{
//^9
class Bullet
{
public:
Bullet(int x, int y) : x_(x), y_(y) {}
bool isOnScreen()
{
return x_ >= 0 && x_ < SCREEN_WIDTH &&
y_ >= 0 && y_ < SCREEN_HEIGHT;
}
void move() { x_ += 5; }
private:
int x_, y_;
};
//^9
}
namespace Singleton9
{
class Log
{
public:
virtual ~Log() {}
virtual void write(const char* text) = 0;
};
//^10
class GameObject
{
protected:
Log& getLog() { return log_; }
private:
static Log& log_;
};
class Enemy : public GameObject
{
void doSomething()
{
getLog().write("I can log!");
}
};
//^10
}
namespace Singleton10
{
class Log {};
class FileSystem {};
class AudioPlayer
{
public:
virtual void play(int id) = 0;
};
//^11
class Game
{
public:
static Game& instance() { return instance_; }
// Functions to set log_, et. al. ...
Log& getLog() { return *log_; }
FileSystem& getFileSystem() { return *fileSystem_; }
AudioPlayer& getAudioPlayer() { return *audioPlayer_; }
private:
static Game instance_;
Log *log_;
FileSystem *fileSystem_;
AudioPlayer *audioPlayer_;
};
//^11
Game Game::instance_ = Game();
void foo()
{
int VERY_LOUD_BANG = 0;
//^12
Game::instance().getAudioPlayer().play(VERY_LOUD_BANG);
//^12
}
}