Decorator(装饰模式)
2012-02-15 00:23
232 查看
#ifndef COMPONENT_H
#define COMPONENT_H
class Component
{
public:
/** Default constructor */
Component();
/** Default destructor */
virtual ~Component();
virtual void Operation()=0;
protected:
private:
};
#endif // COMPONENT_H
#include "Component.h" Component::Component() { //ctor } Component::~Component() { //dtor }
#ifndef CONCRETECOMPONENT_H #define CONCRETECOMPONENT_H #include "Component.h" class ConcreteComponent : public Component { public: /** Default constructor */ ConcreteComponent(); /** Default destructor */ virtual ~ConcreteComponent(); void Operation(); protected: private: }; #endif // CONCRETECOMPONENT_H
#include "ConcreteComponent.h" #include <iostream> using namespace std; ConcreteComponent::ConcreteComponent() { cout<<"生产了一个IPhone手机"<<endl; } ConcreteComponent::~ConcreteComponent() { cout<<"回收了一个IPhone手机"<<endl; } void ConcreteComponent::Operation() { cout<<"IPhone"<<endl; }
#ifndef CONCRETEDECORATORA_H #define CONCRETEDECORATORA_H #include "Decorator.h" class ConcreteDecoratorA : public Decorator { public: /** Default constructor */ ConcreteDecoratorA(Component* pCom); /** Default destructor */ virtual ~ConcreteDecoratorA(); void Operation(); void AddBeHavior(); protected: private: }; #endif // CONCRETEDECORATORA_H
#include "ConcreteDecoratorA.h" #include <iostream> using namespace std; ConcreteDecoratorA::ConcreteDecoratorA(Component* pCom):Decorator(pCom) { cout<<"创建了一个装饰类A"<<endl; } ConcreteDecoratorA::~ConcreteDecoratorA() { cout<<"销毁了一个装饰类A"<<endl; } void ConcreteDecoratorA::Operation() { Decorator::Operation(); AddBeHavior(); } void ConcreteDecoratorA::AddBeHavior() { cout<<"给IPhone添加行为"<<endl; }
#ifndef CONCRETEDECORATORB_H #define CONCRETEDECORATORB_H #include "Decorator.h" #include <iostream> #include <string> using namespace std; class ConcreteDecoratorB : public Decorator { public: /** Default constructor */ ConcreteDecoratorB(Component* pCom); /** Default destructor */ virtual ~ConcreteDecoratorB(); void Operation(); protected: private: string m_AddedState; }; #endif // CONCRETEDECORATORB_H
#include "ConcreteDecoratorB.h" #include <iostream> using namespace std; ConcreteDecoratorB::ConcreteDecoratorB(Component* pCom):Decorator(pCom) { cout<<"创建了一个装饰类B"<<endl; m_AddedState="给IPhone添加了一个状态"; } ConcreteDecoratorB::~ConcreteDecoratorB() { cout<<"销毁了一个装饰类B"<<endl; } void ConcreteDecoratorB::Operation() { Decorator::Operation(); cout<<m_AddedState<<endl; }
#include <iostream> using namespace std; #include "Component.h" #include "ConcreteDecoratorA.h" #include "ConcreteDecoratorB.h" #include "ConcreteComponent.h" int main() { Component* m_pIPhone=new ConcreteComponent(); Component* m_pDa=new ConcreteDecoratorA(m_pIPhone); Component* m_pDb=new ConcreteDecoratorB(m_pDa); m_pDb->Operation(); delete m_pDb; delete m_pDa; delete m_pIPhone; return 0; }
相关文章推荐
- Java设计模式-----Decorator装饰模式
- 设计模式09: Decorator 装饰模式(结构型模式)
- java23种常用设计模式之装饰模式(Decorator)
- JAVA设计模式(7) —<结构型>装饰模式(Decorator)
- 设计模式(八)装饰器模式Decorator(结构型)
- 设计模式_装饰模式(Decorator Pattern)
- 设计模式:装饰模式(Decorator)
- 装饰模式(DECORATOR)案例分析
- Decorator(装饰模式)
- 模式人生-从生活中的点点滴滴认识设计模式1-装饰器模式(Decorator Pattern)
- Decorator装饰(结构型模式)
- 装饰模式(Decorator Pattern)
- 【Unity与23种设计模式】装饰模式(Decorator)
- .NET设计模式(10):装饰模式(Decorator Pattern)
- 装饰模式(Decorator Pattern)
- 装饰模式【Decorator Pattern 】
- 装饰(Decorator)模式
- 装饰模式(Decorator)
- [设计模式]-装饰器模式(Decorator)
- 十、Decorator 装饰(结构型模式)