博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
常见设计模式C++代码实现
阅读量:4608 次
发布时间:2019-06-09

本文共 9445 字,大约阅读时间需要 31 分钟。

1 #include 
2 #include
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_Prototype109 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:End123 124 //// adapter parrten:Begin125 class adp_Adaptee126 { public:127 int simu() { std::cout << "simulation signal." << std::endl; }128 };129 130 class adp_Adapter131 { public:132 virtual void digital() = 0;133 };134 135 class adp_AdapterA136 { 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:End151 152 //// state partten:Start153 enum STA_STATE {STA_START, STA_RUNNING, STA_FINISH, STA_EXIT};154 155 class sta_State156 { public:157 virtual void handle() = 0; 158 };159 160 class sta_ConcreteStateStart: public sta_State161 { public:162 void handle() { std::cout << "Starting..." << std::endl; }163 }; 164 165 class sta_ConcreteStateRunning: public sta_State166 { public:167 void handle() { std::cout << "Running..." << std::endl; }168 }; 169 170 class sta_ConcreteStateFinish: public sta_State171 { public:172 void handle() { std::cout << "Finishing..." << std::endl; }173 }; 174 175 class Context176 {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 } // request208 }; 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:End220 221 //// component parrten:Start222 class com_Component223 { 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_Component231 { public:232 void opt() { std::cout << "leaf" << std::endl; }233 };234 #include
235 #include
236 class com_InnerNode: public com_Component237 { public:238 std::vector
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 } // for245 }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:End262 263 //// observer parrten:Start264 class obs_Observer265 { public:266 virtual void update() = 0;267 };268 269 class obs_ObserverA : public obs_Observer270 { public:271 void update() { std::cout << "obs_ObserverA::update()" << std::endl; }272 };273 274 class obs_Subject275 { 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_Subject282 { 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:End298 299 //// vistor parrten:Start300 class vis_Vistor;301 class vis_Element302 { public:303 virtual void accept(vis_Vistor *vistor) = 0;304 };305 306 class vis_ConcreteElemA : public vis_Element307 { public:308 int dta;309 void accept(vis_Vistor *vistor);310 };311 312 class vis_ConcreteElemB : public vis_Element313 { public:314 int dtb;315 void accept(vis_Vistor *vistor);316 };317 318 class vis_Vistor319 { 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:End338 339 int main(int argc, char *argv[])340 {341 test_vistor();342 343 system("PAUSE");344 return EXIT_SUCCESS;345 }
posted on
2012-10-01 17:51 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/icanth/archive/2012/10/01/2709842.html

你可能感兴趣的文章
C# 备份、还原、拷贝远程文件夹
查看>>
在windows环境下运行compass文件出现的错误提示解决方案
查看>>
CSS常用样式--font
查看>>
恩如氏--蜗牛精华补水蚕丝面膜
查看>>
大工具-收藏
查看>>
codevs3027 线段覆盖 2
查看>>
markdown
查看>>
【leetcode】107-Binary Tree Level Order Traversal II
查看>>
Jquert data方法获取不到数据,显示为undefined。
查看>>
ssm项目中 数据库和资源的备份
查看>>
HDU5950【矩阵快速幂】
查看>>
在线C++编译器
查看>>
C#中各种serialization的比较
查看>>
P2617 Dynamic Rankings
查看>>
工作学习常识1
查看>>
Eclipse插件项目中读取文件
查看>>
jquery定义链接跳转的高亮显示
查看>>
CheckListBox怎样得到多选值?
查看>>
三道题(关于虚表指针位置/合成64位ID/利用栈实现四则运算)
查看>>
Vijos P1243 生产产品 (单调队列优化DP)
查看>>