head File
//DecoratorPattern.h #ifndef DECORATOR_PATTERN_H #define DECORATOR_PATTERN_H #include <Windows.h> #include <iostream> using namespace std; namespace DecoratorPattern { /* ** FileName : DecoratorPatternDemo ** Author : Jelly Young ** Date : 2013/12/19 ** Description : More information, please go to http://www.jellythink.com */ class Component { public: Component(); virtual ~Component(); virtual void Operation() = 0; }; class ConcreteComponent : public Component { public: ConcreteComponent(); virtual ~ConcreteComponent(); virtual void Operation(); }; // ////////////////////////////////////////////////////////////////////////// class Decorator : public Component { public: Decorator(Component* pComponent); virtual ~Decorator(); virtual void Operation(); protected: Component* m_pComponentObj; }; class ConcreteDecoratorA : public Decorator { public: ConcreteDecoratorA(Component* pDecorator) ; virtual ~ConcreteDecoratorA(); virtual void Operation(); void AddedBehavior(); }; class ConcreteDecoratorB : public Decorator { public: ConcreteDecoratorB(Component* pDecorator); virtual ~ConcreteDecoratorB(); virtual void Operation(); void AddedBehavior(); }; ////////////////////////////////////////////////////////////////////////// void DecoratorPattern_Test(); } #endif
accomplish
#include "DecoratorPattern.h" namespace DecoratorPattern { Component::Component() { } Component::~Component() { } // ////////////////////////////////////////////////////////////////////////// ConcreteComponent::ConcreteComponent() { } ConcreteComponent::~ConcreteComponent() { } void ConcreteComponent::Operation() { cout<<"I am no decoratored ConcreteComponent"<<endl; } // ////////////////////////////////////////////////////////////////////////// Decorator::Decorator(Component *pComponent) : m_pComponentObj(pComponent) {} Decorator::~Decorator() {} void Decorator::Operation() { if (m_pComponentObj != NULL) { m_pComponentObj->Operation(); } } ////////////////////////////////////////////////////////////////////////// ConcreteDecoratorA::ConcreteDecoratorA(Component* pDecorator) : Decorator(pDecorator) {} ConcreteDecoratorA::~ConcreteDecoratorA() {} void ConcreteDecoratorA::Operation() { AddedBehavior (); Decorator::Operation(); } void ConcreteDecoratorA::AddedBehavior() { cout<<"This is added behavior A."<<endl; } ConcreteDecoratorB::ConcreteDecoratorB(Component *pDecorator) : Decorator(pDecorator) {} ConcreteDecoratorB::~ConcreteDecoratorB() {} void ConcreteDecoratorB::Operation() { AddedBehavior (); Decorator::Operation(); } void ConcreteDecoratorB::AddedBehavior() { cout<<"This is added behavior B."<<endl; } ////////////////////////////////////////////////////////////////////////// void DecoratorPattern_Test() { Component *pComponentObj = new ConcreteComponent(); Decorator *pDecoratorAOjb = new ConcreteDecoratorA(pComponentObj); pDecoratorAOjb->Operation(); cout<<"============================================="<<endl; Decorator *pDecoratorBOjb = new ConcreteDecoratorB(pComponentObj); pDecoratorBOjb->Operation(); cout<<"============================================="<<endl; Decorator *pDecoratorBAOjb = new ConcreteDecoratorB(pDecoratorAOjb); pDecoratorBAOjb->Operation(); cout<<"============================================="<<endl; delete pDecoratorBAOjb; pDecoratorBAOjb = NULL; delete pDecoratorBOjb; pDecoratorBOjb = NULL; delete pDecoratorAOjb; pDecoratorAOjb = NULL; delete pComponentObj; pComponentObj = NULL; } }
client
#include "DecoratorPattern.h" #include <iostream> using namespace std; using namespace DecoratorPattern; void main() { DecoratorPattern_Test(); }
operation result