设计模式读书笔记:Decorator(装饰)
来源:互联网 发布:淘宝查号 编辑:程序博客网 时间:2024/06/07 22:55
意图:
动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。
结构图:来自 《23种设计模式 - 郗晓勇》
实现:https://github.com/panshiqu/patterns/tree/master/Decorator
Component
namespace NS_DECORATOR {class Component {public:Component() {}virtual ~Component() {}virtual void operation(void) = 0;};} /* namespace NS_DECORATOR */ConcreteComponent
#include "Component.h"#include <iostream>namespace NS_DECORATOR {class ConcreteComponent : public Component{public:ConcreteComponent() {}virtual ~ConcreteComponent() {}virtual void operation(void){std::cout << "ConcreteComponent" << std::endl;}};} /* namespace NS_DECORATOR */Decorator
#include "Component.h"namespace NS_DECORATOR {class Decorator : public Component{public:Decorator(Component *component) : _component(component) {}virtual ~Decorator() {}virtual void operation(void){_component->operation();}private:Component *_component;};} /* namespace NS_DECORATOR */ConcreteDecoratorA
#include "Decorator.h"#include <iostream>namespace NS_DECORATOR {class ConcreteDecoratorA : public Decorator{public:ConcreteDecoratorA(Component *component) : Decorator(component) {}virtual ~ConcreteDecoratorA() {}virtual void operation(void){Decorator::operation();addBehavior();}void addBehavior(void){std::cout << "Jump" << std::endl;}};} /* namespace NS_DECORATOR */ConcreteDecoratorB
#include "Decorator.h"#include <iostream>namespace NS_DECORATOR {class ConcreteDecoratorB : public Decorator{public:ConcreteDecoratorB(Component *component) : Decorator(component) {}virtual ~ConcreteDecoratorB() {}virtual void operation(void){Decorator::operation();addBehavior();}void addBehavior(void){std::cout << "Move" << std::endl;}};} /* namespace NS_DECORATOR */main
#include "Decorator/ConcreteComponent.h"#include "Decorator/ConcreteDecoratorA.h"#include "Decorator/ConcreteDecoratorB.h"using namespace NS_DECORATOR;using namespace std;int main(void){ConcreteComponent cc;cc.operation();cout << "----------" << endl;ConcreteDecoratorA cda(&cc);cda.operation();cout << "----------" << endl;ConcreteDecoratorB cdb(&cda);cdb.operation();}附加:
0 0
- 设计模式读书笔记:Decorator(装饰)
- 设计模式 - 装饰 Decorator
- Decorator(装饰)设计模式
- 设计模式读书笔记之装饰器模式(Decorator)
- 读书笔记 HeadFirst设计模式之装饰器(Decorator)模式
- 设计模式读书笔记之装饰器模式(Decorator)
- 设计模式-装饰模式(Decorator Pattern)
- 设计模式---装饰模式(decorator)
- 设计模式 (十)装饰模式(Decorator)
- 设计模式 (十)装饰模式(Decorator)
- 设计模式 (十)装饰模式(Decorator)
- 【设计模式】之装饰模式(Decorator)
- 设计模式之装饰模式(Decorator)
- 设计模式之 装饰模式(Decorator)
- 设计模式之装饰模式(Decorator)
- Java设计模式---装饰模式(Decorator)
- 设计模式(五):装饰模式(Decorator)
- 设计模式(7)--Decorator 装饰模式
- java泛型的理解
- 使用ViewPager实现左右循环滑动
- OpenStack 2015.1.0 (Kilo)更新日志
- hiho一下 第五十二周(割边 割点)
- Calendar 中getActualMaximumd 功能
- 设计模式读书笔记:Decorator(装饰)
- leetcode 日经贴,Cpp code -Palindrome Linked List
- json格式封装,以及格式转换
- UVa 11137 Ingenuous Cubrency
- 黑马程序员——Java基础---集合
- 新建和配置Phalcon工程
- No unique bean of type is defined: expected single bean but found 0
- 文章标题
- opencv源码之一:cvboost.cpp