大话设计模式之修饰模式
2016-08-19 16:08
453 查看
动态的给一个对象添加额外的职责,就增加功能而言,装饰模式比生成子类更为灵活。
//设计模式学习 class Compent{ public: virtual void dipFushi() = 0 ; protected: }; class Person : public Compent{ public: Person(string str): str_name(str){ } virtual void dipFushi(){ cout <<str_name<< "装扮:"<<endl; } protected: string str_name; }; class Fushi : public Compent{ public: void SetCompent(Compent *pcom){ this->pCompent = pcom; } virtual void dipFushi(){ if (NULL != pCompent) { pCompent->dipFushi(); } } protected: Compent *pCompent; }; class Yifu : public Fushi{ public: Yifu(string str): str_name(str){ } virtual void dipFushi(){ Fushi::dipFushi(); cout << str_name << " "; } private: string str_name; }; int main() { int n1 =9,n2=3; Yifu *f1 = new Yifu("大衣"); Yifu *f2 = new Yifu("围巾"); Yifu *f3 = new Yifu("短裤"); Yifu *f4 = new Yifu("靴子"); Yifu *f5 = new Yifu("皮鞋"); Yifu *f6 = new Yifu("公文包"); Yifu *f7 = new Yifu("西服"); Yifu *f8 = new Yifu("领带"); Person *p1 = new Person("小明"); Person *p2 = new Person("小菜"); f1->SetCompent(p1); f2->SetCompent(f1); f2->dipFushi(); f7->SetCompent(p2); f8->SetCompent(f7); f3->SetCompent(f8); f5->SetCompent(f3); f6->SetCompent(f5); f6->dipFushi(); system("pause"); return 0; }
相关文章推荐
- 《大话设计模式》第29章-OOTV杯超级模式大赛—模式总结(七 大结局,附本书序、前言和样章)
- 《大话设计模式》第29章-OOTV杯超级模式大赛—模式总结(一)
- 读《大话设计模式》---外观模式(Facade)
- 《大话设计模式》读书笔记一 简单工厂模式
- 2009-05-18读书记录:《大话设计模式》第十六章——状态模式
- 2009-04-27读书记录:《大话设计模式》第七章——代理模式
- 读《大话设计模式》---观察者模式(Observer)
- 大话设计模式之C++实现---简单工厂模式
- 2009-04-12读书记录:《大话设计模式》第二十一章——单例模式在多线程的应用
- 《大话设计模式》第29章-OOTV杯超级模式大赛—模式总结(二)
- 读《大话设计模式》和《head first 设计模式》心得
- 读《大话设计模式》---代理模式(proxy)
- 读《大话设计模式》---模板方法模式(TemplateMethond)
- 设计模式总结(1)(内容来源与大话设计模式)
- 《大话设计模式》第29章-OOTV杯超级模式大赛—模式总结(三)
- 《大话设计模式》第29章-OOTV杯超级模式大赛—模式总结(五)
- 读《大话设计模式》和《head first 设计模式》心得
- 设计模式之一——Decorator(修饰)
- 2009-05-14读书记录:《大话设计模式》第十八章——备忘录模式
- 2009-05-19读书记录:《大话设计模式》第九章——原型模式