常见设计模式C++代码实现

  1 #include <cstdlib>

  2 #include <iostream>

  3 

  4 using namespace std;

  5 

  6 //// brige partten:Begin

  7 class bri_Implementor

  8 { 

  9 public:

 10          virtual void optImpl() = 0;

 11 };

 12 

 13 class bri_ImplementorA: public bri_Implementor

 14 { public:

 15          void optImpl() { std::cout << "ImplementA" << std::endl; }       

 16 };

 17 

 18 class bri_Abstract

 19 { public:

 20          bri_Implementor *impl;

 21          void opt() { this->impl->optImpl(); }

 22 };

 23 

 24 void test_bridge()

 25 {

 26      bri_Abstract a;

 27      a.impl = new bri_ImplementorA();

 28      a.opt();   

 29 }

 30 //// brige partten:End

 31 

 32 //// single partten:Begin

 33 class sig_Singlon

 34 { 

 35 public:

 36       int data;

 37       static sig_Singlon * inst;

 38       

 39       static sig_Singlon& instance()

 40       {

 41              if(inst != NULL) {

 42                      return *inst;

 43              }

 44              else {

 45                  inst = new sig_Singlon();

 46                  inst->data = 3;                 

 47                  return *inst;

 48              }

 49       } // instance

 50       

 51       int get_data() { return this->data; }

 52 };

 53 //// single partten:End

 54 

 55 sig_Singlon* sig_Singlon::inst = NULL;

 56 

 57 void test_singlon()

 58 {

 59      std::cout << "Singlon Data is: " <<

 60                sig_Singlon::instance().get_data() << std::endl;     

 61 }

 62 

 63 //// builder partten:Begin

 64 class bui_Product

 65 { public:

 66 };

 67 

 68 class bui_Builder

 69 { public:

 70          virtual void buildPartA() = 0;

 71          virtual void buildPartB() = 0;

 72          virtual bui_Product getResult() = 0;

 73 };

 74 

 75 class bui_BuilderA : public bui_Builder

 76 { public:

 77          void buildPartA(){ std::cout << "BuildA::PartA" << std::endl; }

 78          void buildPartB(){ std::cout << "BuildA::PartB" << std::endl; }

 79          bui_Product getResult(){std::cout << "Build Finish!~" << std::endl;} 

 80 };

 81 

 82 class bui_Director

 83 { public:

 84          static bui_Product construct(bui_Builder * b) {

 85               b->buildPartA();

 86               b->buildPartB();

 87               return b->getResult();     

 88          } // construct       

 89 };

 90 

 91 void test_builder()

 92 {

 93      bui_Director::construct(new bui_BuilderA());

 94 }

 95 //// builder partten:End

 96 

 97 //// prototype parrten:Begin

 98 class proto_Prototype

 99 { public:

100        int *pdata;

101        proto_Prototype(int data = 0) { pdata = new int(data); }

102        proto_Prototype& clone()

103        {

104               proto_Prototype* c = new proto_Prototype();             

105               *(c->pdata) = *(this->pdata); // deeply copy.

106               return (*c);

107        } // clone           

108 }; // proto_Prototype

109 

110 void test_prototype()

111 {

112      proto_Prototype o1;

113      *(o1.pdata) = 1;

114      proto_Prototype o2(o1);

115      proto_Prototype o3 = o1.clone();

116      *(o2.pdata) = 2;

117      *(o3.pdata) = 3;

118      std::cout << "prototype object1 : " << *(o1.pdata) <<

119      " , object2: " << *(o2.pdata) <<

120      ", object3: " << *(o3.pdata) << std::endl;

121 }

122 //// prototype parrten:End

123 

124 //// adapter parrten:Begin

125 class adp_Adaptee

126 { public:

127          int simu() { std::cout << "simulation signal." << std::endl; }

128 };

129 

130 class adp_Adapter

131 { public:

132          virtual void digital() = 0;

133 };

134 

135 class adp_AdapterA

136 { public:

137          adp_Adaptee *adaptee;

138          void digital() { 

139               adaptee->simu(); 

140               std::cout << "convert to digital signal now!" << std::endl; 

141          }

142 };

143 

144 void test_adapter()

145 {

146      adp_AdapterA adp;

147      adp.adaptee = new adp_Adaptee();

148      adp.digital();

149 }

150 //// adapter parrten:End

151 

152 //// state partten:Start

153 enum STA_STATE {STA_START, STA_RUNNING, STA_FINISH, STA_EXIT};

154 

155 class sta_State

156 { public:

157      virtual void handle() = 0;        

158 };

159 

160 class sta_ConcreteStateStart: public sta_State

161 { public:

162      void handle() { std::cout << "Starting..." << std::endl; }

163 }; 

164 

165 class sta_ConcreteStateRunning: public sta_State

166 { public:

167      void handle() { std::cout << "Running..." << std::endl; }

168 }; 

169 

170 class sta_ConcreteStateFinish: public sta_State

171 { public:

172      void handle() { std::cout << "Finishing..." << std::endl; }

173 }; 

174 

175 class Context

176 {

177       enum STA_STATE state;

178       sta_State *handler;

179 public:      

180       Context() { state = STA_START; handler = NULL; }

181       void request()

182       {

183            switch(state)

184            {

185              case STA_START:{ 

186                   handler = new sta_ConcreteStateStart();

187                   handler->handle();

188                   delete handler; 

189                   state = STA_RUNNING;

190              } break;

191              case STA_RUNNING:{ 

192                   handler = new sta_ConcreteStateRunning();                  

193                   handler->handle();

194                   delete handler; 

195                   state = STA_FINISH;

196              } break;

197              case STA_FINISH:{ 

198                   handler = new sta_ConcreteStateFinish();

199                   handler->handle();

200                   delete handler;                   

201                   state = STA_EXIT;

202              } break;

203              default: {             

204                    return;

205              }

206            } // switch             

207       } // request

208 }; 

209 

210 void test_state()

211 {

212      Context context;

213      context.request();

214      context.request();

215      context.request();

216      context.request();    

217      context.request();    

218 }

219 //// state partten:End

220 

221 //// component parrten:Start

222 class com_Component

223 { public:

224       virtual void opt() = 0;

225       virtual void add(com_Component *c) {}

226       virtual void remove(com_Component *c) {}

227       virtual com_Component* getChild(int i) { return NULL; }

228 };

229 

230 class com_Leaf: public com_Component

231 { public:

232       void opt() { std::cout << "leaf" << std::endl; }

233 };

234 #include <vector>

235 #include <algorithm>

236 class com_InnerNode: public com_Component

237 { public:

238       std::vector<com_Component *> node_set;

239       void opt()

240       {

241            for(int i = 0; i < node_set.size(); ++i)

242            {

243                    std::cout << "inner->"; node_set[i]->opt();

244            } // for

245       }

246       void add(com_Component *c) { node_set.push_back(c); }

247       void remove(com_Component *c) { 

248            node_set.erase(std::remove(node_set.begin(), node_set.end(), c), node_set.end());

249       }

250       com_Component* getChild(int i) { return node_set[i]; }

251 };

252 

253 void test_component()

254 {

255      com_InnerNode *root = new com_InnerNode();

256      root->add(new com_InnerNode());

257      root->getChild(0)->add(new com_Leaf());

258      root->getChild(0)->add(new com_Leaf());

259      root->opt();

260 }

261 //// component parrten:End

262 

263 //// observer parrten:Start

264 class obs_Observer

265 { public:

266       virtual void update() = 0;

267 };

268 

269 class obs_ObserverA : public obs_Observer

270 { public:

271       void update() { std::cout << "obs_ObserverA::update()" << std::endl; }

272 };

273 

274 class obs_Subject

275 { public:

276       virtual void attach(obs_Observer *o) = 0;

277       virtual void detach(obs_Observer *&o) = 0;

278       virtual void notify() = 0;

279 };

280 

281 class obs_SubjectImplA:public obs_Subject

282 { public:      

283       obs_Observer *inst;

284       void attach(obs_Observer *o) { inst = o; }

285       void detach(obs_Observer *&o) { o = inst; inst = NULL; }

286       void notify() { inst->update(); }

287 };

288 

289 void test_observer()

290 {

291      obs_SubjectImplA subject;

292      subject.attach(new obs_ObserverA());

293      subject.notify();

294      obs_Observer *observer = NULL;

295      subject.detach(observer);

296 }

297 //// observer parrten:End

298 

299 //// vistor parrten:Start

300 class vis_Vistor;

301 class vis_Element

302 { public:

303       virtual void accept(vis_Vistor *vistor) = 0;

304 };

305 

306 class vis_ConcreteElemA : public vis_Element

307 { public:

308       int dta;

309       void accept(vis_Vistor *vistor);

310 };

311 

312 class vis_ConcreteElemB : public vis_Element

313 { public:

314       int dtb;

315       void accept(vis_Vistor *vistor);

316 };

317 

318 class vis_Vistor

319 { public:

320       void vistElemA(vis_ConcreteElemA *a) { a->dta = 911; }

321       void vistElemB(vis_ConcreteElemB *b) { b->dtb = 101; }

322 };

323 

324 void vis_ConcreteElemA::accept(vis_Vistor *vistor) { vistor->vistElemA(this); }

325 void vis_ConcreteElemB::accept(vis_Vistor *vistor) { vistor->vistElemB(this); }

326 

327 void test_vistor()

328 {

329      vis_ConcreteElemA a;

330      vis_ConcreteElemB b;

331      vis_Vistor *vistor = new vis_Vistor();

332      a.accept(vistor);

333      b.accept(vistor);

334      std::cout << "ElemA's Value:" << a.dta 

335                << " ,ElemB's Value:" << b.dtb << std::endl;     

336 }

337 //// vistor parrten:End

338 

339 int main(int argc, char *argv[])

340 {

341     test_vistor();

342     

343     system("PAUSE");

344     return EXIT_SUCCESS;

345 }

你可能感兴趣的:(设计模式)