装饰模式 为动态的给一个对象添加一些额外的职责
可以有效的把类的核心职责和装饰功能分开,从而去除相关类中重复的装饰逻辑
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person()
{
}
Person(string name1)
{
name = name1;
}
virtual void show()
{
cout<<"装扮的"<<name;
}
private:
string name;
};
class Finery:public Person
{
protected:
Person *component;
public:
void Decorate(Person *person)
{
component = person;
}
virtual void show()
{
if(component != NULL)
component ->show();
}
};
class TShirts:public Finery
{
public:
void show()
{
cout<<"大T血衫";
Finery::show();
}
};
class BigTrouser : public Finery
{
public:
void show()
{
cout<<"垮裤";
Finery::show();
}
};
int main()
{
Person *xc = new Person("小菜");
cout<<"第一种装扮"<<endl;
TShirts *shirt = new TShirts();
BigTrouser *trourser = new BigTrouser();
shirt->Decorate(xc);
trourser->Decorate(shirt);
trourser->show();
delete trourser;
delete shirt;
delete xc;
return 0;
}