设计模式c++实现(一):装饰(Decorator)模式
来源:互联网 发布:进口美工刀架品牌 编辑:程序博客网 时间:2024/06/17 16:51
转载请注明,来自:http://blog.csdn.net/skyman_2001
class Component{public:Component() {}virtual ~Component() {}virtual void operation() = 0;virtual Component *remove() = 0;};class ConcreteComponent: public Component{public:ConcreteComponent() {}~ConcreteComponent() {}void operation(){std::cout <<"Concrete component operation"<< std::endl;}Component *remove(){return this;}};
class Decorator : public Component{public:Decorator(Component *comp) : m_component(comp) {}virtual ~Decorator() {} virtual void operation(){m_component->operation();}Component *remove(){return m_component;} private:Component *m_component;};class Decor1: public Decorator{public:Decor1(Component *comp) : Decorator(comp){}virtual ~Decor1() {}virtual void operation(){Decorator::operation();addedBehavior();}private:void addedBehavior(){std::cout << "Decor1 addedBehavior" << std::endl;}};class Decor2: public Decorator{public:Decor2(Component *comp) : Decorator(comp) {}virtual ~Decor2() {}virtual void operation(){Decorator::operation();addedBehavior();}private:void addedBehavior(){std::cout << "Decor2 addedBehavior" << std::endl;}};
int _tmain(int argc, _TCHAR* argv[]){Component* concrete = new ConcreteComponent();Component* decored1 = new Decor1(concrete);Component* decored2 = new Decor2(decored1);decored2->operation();cout << "---------------" << endl;Component* temp = decored2->remove();temp->operation();cout << "---------------" << endl;temp = temp->remove();temp->operation();cout << "---------------" << endl;temp = temp->remove();temp->operation();delete decored2;delete decored1;delete concrete;return 0;}
运行结果:
Concrete component operation
Decor1 addedBehavior
Decor2 addedBehavior
---------------
Concrete component operation
Decor1 addedBehavior
---------------
Concrete component operation
---------------
Concrete component operation
0 0
- 设计模式c++实现(一):装饰(Decorator)模式
- 设计模式学习(一)---装饰着模式(Decorator)
- 设计模式一日一练:装饰者模式(Decorator)
- (C++)设计模式------装饰者模式 decorator
- 设计模式 - 装饰 Decorator
- Decorator(装饰)设计模式
- 设计模式-装饰模式(Decorator Pattern)
- 设计模式---装饰模式(decorator)
- 设计模式 (十)装饰模式(Decorator)
- 设计模式 (十)装饰模式(Decorator)
- 设计模式 (十)装饰模式(Decorator)
- 【设计模式】之装饰模式(Decorator)
- 设计模式之装饰模式(Decorator)
- 设计模式之 装饰模式(Decorator)
- 设计模式之装饰模式(Decorator)
- Java设计模式---装饰模式(Decorator)
- 设计模式(五):装饰模式(Decorator)
- 设计模式(7)--Decorator 装饰模式
- 函数索引
- 操作符重载
- Linux下删除文件恢复
- GDB 命令详细解释
- 乘法逆元
- 设计模式c++实现(一):装饰(Decorator)模式
- 12.11上午会面总结ExpandableListView
- JavaScript高级之闭包的概念及其应用
- Sping MVC + Freemarker实现表单提交
- GridView问题集合
- static和extern
- 给正在读硕士阶段学生的小建议
- Windows游戏编程 - 简单的弹球窗口
- DB2监控