[构造型模式] 装饰者模式的理解
[结构型模式] 装饰者模式的理解
![[构造型模式] 装饰者模式的理解 [构造型模式] 装饰者模式的理解](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDE2LzA1LzE4LzE3MjMzMjE2NjIuanBn)
![[构造型模式] 装饰者模式的理解 [构造型模式] 装饰者模式的理解](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDE2LzA1LzE4LzE3MjMzMjE2NjMuanBn)
头文件
实现
客户端
运行结果
头文件
//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
实现
#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; } }
客户端
#include "DecoratorPattern.h" #include <iostream> using namespace std; using namespace DecoratorPattern; void main() { DecoratorPattern_Test(); }
运行结果