#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person()
{
}
Person(string nameStr)
{
this->name = nameStr;
}
virtual void show()
{
cout<<name<<"的装扮"<<endl;
}
private:
string name;
};
class Finery:public Person
{
public:
Finery()
{
}
void Decorate(Person* component)
{
this->component = component;
}
void show() override
{
component->show();
}
protected:
Person* component;
};
class TShirts: public Finery
{
public:
TShirts()
{
}
void show() override
{
component->show();
cout<<"穿T恤"<<endl;
}
};
class BigTrouser: public Finery
{
public:
BigTrouser()
{
}
void show() override
{
component->show();
cout<<"穿垮裤"<<endl;
}
};
int main()
{
Person* pe = new Person("小菜");
TShirts* ts = new TShirts();
BigTrouser* bt = new BigTrouser();
ts->Decorate(pe);
bt->Decorate(ts);
bt->show();
getchar();
return 0;
}
![c++ 装饰模式 - 1307520486 - 1307520486的博客 c++ 装饰模式 - 1307520486 - 1307520486的博客](https://i-blog.csdnimg.cn/blog_migrate/cce544e7261bf3a4535656c82b969b03.png)
![c++ 装饰模式 - 1307520486 - 1307520486的博客 c++ 装饰模式 - 1307520486 - 1307520486的博客](https://i-blog.csdnimg.cn/blog_migrate/cce544e7261bf3a4535656c82b969b03.png)