【初学设计模式】Decorator (装饰)
2011-03-17 20:09
351 查看
// 所有原创文章转载请注明作者及链接
// blackboycpp(AT)gmail.com
// QQ群: 135202158
类图:
代码实现:
类声明:
类实现:
main():
// blackboycpp(AT)gmail.com
// QQ群: 135202158
类图:
代码实现:
类声明:
/******************************************************************** File: decorator.h Author: blackboy blackboycpp@gmail.com Purpose: 装饰模式 Created: 2011-03-16 Modified: 2011-03-16 19:48 *********************************************************************/ #ifndef __DECORATOR_H__ #define __DECORATOR_H__ namespace blackboycpp { ////////////////////////////////////////////////////////////////////////// class Component { public: virtual void Operation() = 0; }; class ConcreteComponent : public Component { public: virtual void Operation(); }; ////////////////////////////////////////////////////////////////////////// class Decorator : public Component { public: Decorator(); Decorator(Component*); virtual void Operation(); private: Component* _component; }; class ConcreteDecoratorA : public Decorator { public: ConcreteDecoratorA(); ConcreteDecoratorA(Component*); void Operation(); private: void AddedState(); }; class ConcreteDecoratorB : public Decorator { public: ConcreteDecoratorB(); ConcreteDecoratorB(Component*); void Operation(); private: void AddedBehavior(); }; } #endif
类实现:
#include <iostream> #include "decorator.h" namespace blackboycpp { ////////////////////////////////////////////////////////////////////////// void ConcreteComponent::Operation() { std::cout << "ConcreteComponent::Operation()/n"; } ////////////////////////////////////////////////////////////////////////// Decorator::Decorator() { std::cout << "Decorator::Decorator()/n"; _component = 0; } Decorator::Decorator(Component* p) { std::cout << "Decorator::Decorator(Component*)/n"; _component = p; } void Decorator::Operation() { std::cout << "Decorator::Operation()/n"; _component->Operation(); } ////////////////////////////////////////////////////////////////////////// ConcreteDecoratorA::ConcreteDecoratorA() : Decorator() { std::cout << "ConcreteDecoratorA::ConcreteDecoratorA()/n"; } ConcreteDecoratorA::ConcreteDecoratorA(Component* p) : Decorator(p) { std::cout << "ConcreteDecoratorA::ConcreteDecoratorA(Component*)/n"; } void ConcreteDecoratorA::Operation() { std::cout << "ConcreteDecoratorA::Operation()/n"; Decorator::Operation(); AddedState(); } void ConcreteDecoratorA::AddedState() { std::cout << "ConcreteDecoratorA::AddedState()/n"; } ////////////////////////////////////////////////////////////////////////// ConcreteDecoratorB::ConcreteDecoratorB() : Decorator() { std::cout << "ConcreteDecoratorB::ConcreteDecoratorB()/n"; } ConcreteDecoratorB::ConcreteDecoratorB(Component* p) : Decorator(p) { std::cout << "ConcreteDecoratorB::ConcreteDecoratorB(Component*)/n"; } void ConcreteDecoratorB::Operation() { std::cout << "ConcreteDecoratorB::Operation()/n"; Decorator::Operation(); AddedBehavior(); } void ConcreteDecoratorB::AddedBehavior() { std::cout << "ConcreteDecoratorB::AddedBehavior()/n"; } }
main():
#include <cstdlib> #include "decorator.h" using namespace blackboycpp; int main(int argc, char** argv) { Component* component = new ConcreteComponent; ConcreteDecoratorA* decoratorA = new ConcreteDecoratorA(component); ConcreteDecoratorB* decoratorB = new ConcreteDecoratorB(decoratorA); decoratorB->Operation(); delete decoratorB; delete decoratorA; delete component; system("pause"); return 0; }
相关文章推荐
- 初学设计模式【3】装饰模式——Decorator
- 设计模式:装饰模式(Decorator )
- java设计模式:装饰器模式[Decorator]
- java23种设计模式之装饰模式(Decorator)
- 研磨设计模式 之 装饰模式(Decorator)1
- 设计模式(3)-结构型-装饰模式(Decorator)
- 23种设计模式之装饰模式(Decorator)
- Java设计模式-装饰模式(Decorator)
- 设计模式之(三、装饰模式Decorator)
- 设计模式(三)The Decorator Pattern 装饰器模式
- 模式人生-从生活中的点点滴滴认识设计模式1-装饰器模式(Decorator Pattern)
- JAVA设计模式(11):结构型-装饰模式(Decorator)
- JAVA--装饰模式(Decorator)--设计模式七
- 极速理解设计模式系列:23.装饰器模式(Decorator Pattern)
- 小鸦之设计模式(4) - 装饰模式Decorator pattern
- 设计模式之装饰模式(Decorator)
- 设计模式学习笔记(三)装饰模式(Decorator)
- 设计模式之装饰模式(Decorator)
- android设计模式-装饰模式(Decorator Pattern)
- java设计模式之装饰模式Decorator