通过嵌套可以随意的添加特征
#include <cstdlib>
#include <iostream>
using namespace std;
/*class Component
{
public:
virtual Operation() = 0;
};
class ConcreteComponent:public Component
{
public:
virtual Operation(){...}
};
class Decorator:Component
{
protected:
Component* m_Com;
public:
Decorator(Component* p):m_Com(p){}
virtual Operation(){...}
};
class ConcreteDecorator:public Decorator
{
public:
ConcreteDecorator(Component* p):Decorator(p){}
virtual Operation()
{
m_Com->Operation();
...
}
};*/
class Component
{
public:
virtual void Do() = 0;
};
class Girl:public Component
{
public:
virtual void Do(){cout<<"I am a beauty girl"<<endl;}
};
class Decorator:public Component
{
protected:
Component* m_pCom;
public:
Decorator(Component* p):m_pCom(p){}
virtual void Do(){
m_pCom->Do();
}
};
class RedHat:public Decorator
{
public:
RedHat(Component* p):Decorator(p){}
virtual void Do(){Decorator::Do();cout<<"I have a Red hat"<<endl;}
};
class Whiteshoot:public Decorator
{
public:
Whiteshoot(Component* p):Decorator(p){}
virtual void Do(){Decorator::Do();cout<<"I have a Whiteshoot"<<endl;}
};
class GreenSkirt:public Decorator
{
public:
GreenSkirt(Component* p):Decorator(p){}
virtual void Do(){Decorator::Do();cout<<"I have a GreenSkirt"<<endl;}
};
int main(int argc, char *argv[])
{
Component* pGirl = new GreenSkirt(new Whiteshoot(new RedHat(new Girl)));
pGirl->Do();
system("PAUSE");
return EXIT_SUCCESS;
}