Decorator 装饰

通过嵌套可以随意的添加特征

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值