Singleton

  1 //Singleton.h
2
3
4
5 #ifndef _SINGLETON_H_
6
7 #define _SINGLETON_H_
8
9
10
11 #include <iostream>
12
13 using namespace std;
14
15
16
17 class Singleton
18
19 {
20
21 public:
22
23 static Singleton* Instance();
24
25
26
27 virtual void PrintInfo();
28
29
30
31 protected:
32
33 Singleton();
34
35
36
37 private:
38
39 static Singleton* _instance;
40
41
42
43 };
44
45
46
47 class SingletonDeriveA:public Singleton
48
49 {
50
51 private:
52
53 friend class Singleton;
54
55 SingletonDeriveA();
56
57 public:
58
59 virtual ~SingletonDeriveA();
60
61
62
63 void PrintInfo();
64
65
66
67 };
68
69
70
71 class SingletonDeriveB:public Singleton
72
73 {
74
75 private:
76
77 friend class Singleton;
78
79 SingletonDeriveB();
80
81
82
83 public:
84
85 virtual ~SingletonDeriveB();
86
87
88
89 void PrintInfo();
90
91
92
93 };
94
95
96
97 char* GetSingletionType();
98
99
100
101 #endif //~_SINGLETON_H_
102
103 //Singleton.cpp
104
105
106
107 #include "Singleton.h"
108
109
110
111 #include <ctime> //for time
112
113
114
115 #include <iostream>
116
117 using namespace std;
118
119
120
121 Singleton* Singleton::_instance = 0;
122
123
124
125 Singleton::Singleton()
126
127 {
128
129 cout<<"Singleton...."<<endl;
130
131 }
132
133
134
135 Singleton* Singleton::Instance()
136
137 {
138
139 const char* type = GetSingletionType();
140
141
142
143 if (_instance == 0)
144
145 {
146
147 if (strcmp(type,"SingletonDeriveA") == 0)
148
149 {
150
151 _instance = new SingletonDeriveA();
152
153 }
154
155 else if (strcmp(type,"SingletonDeriveB") == 0)
156
157 {
158
159 _instance = new SingletonDeriveB();
160
161 }
162
163 else
164
165 {
166
167 _instance = new Singleton();
168
169 }
170
171 }
172
173
174
175 return _instance;
176
177 }
178
179
180
181 void Singleton::PrintInfo()
182
183 {
184
185 cout<<"Singleton type:Singleton"<<endl;
186
187 }
188
189
190
191 SingletonDeriveA::SingletonDeriveA()
192
193 {
194
195 cout<<"SingletonDeriveA...."<<endl;
196
197 }
198
199
200
201 SingletonDeriveA::~SingletonDeriveA()
202
203 {
204
205
206
207 }
208
209
210
211 void SingletonDeriveA::PrintInfo()
212
213 {
214
215 cout<<"Singleton type:SingletonDeriveA"<<endl;
216
217 }
218
219
220
221 SingletonDeriveB::SingletonDeriveB()
222
223 {
224
225 cout<<"SingletonDeriveB...."<<endl;
226
227 }
228
229
230
231 SingletonDeriveB::~SingletonDeriveB()
232
233 {
234
235
236
237 }
238
239
240
241 void SingletonDeriveB::PrintInfo()
242
243 {
244
245 cout<<"Singleton type:SingletonDeriveB"<<endl;
246
247 }
248
249
250
251
252
253 char* GetSingletionType()
254
255 {
256
257 //随机返回一个Singleton子类
258
259 srand((unsigned int)time(NULL));
260
261 int SINGLETON_TYPE = rand() % 3;
262
263
264
265 switch (SINGLETON_TYPE)
266
267 {
268
269 case 0:
270
271 return "SingletonDeriveA";
272
273 break;
274
275 case 1:
276
277 return "SingletonDeriveB";
278
279 break;
280
281 case 2:
282
283 return "Singleton";
284
285 break;
286
287 default :
288
289 return "Singleton";
290
291 break;
292
293 }
294
295 }



posted @ 2012-02-02 11:38  linyliny  阅读(168)  评论(0)    收藏  举报