代码拉取完成,页面将自动刷新
#include <iostream>
#include <string>
#include <memory>
//装饰模式
class Finery
{
public:
using ptr=std::shared_ptr<Finery>;
virtual void show()=0;
virtual ~Finery() {};
};
class ConcrateFinery:public Finery
{
public:
using ptr=std::shared_ptr<ConcrateFinery>;
virtual void show() override
{
std::cout<<"hello"<<std::endl;
}
virtual ~ConcrateFinery() {};
};
class Person:public Finery
{
public:
using ptr=std::shared_ptr<Person>;
Person(std::string name):m_name(name)
{}
virtual void show() override
{
std::cout<<"装扮的"<<this->m_name<<std::endl;
}
private:
std::string m_name;
};
class TShirts:public Finery
{
public:
using ptr=std::shared_ptr<TShirts>;
TShirts(Finery::ptr f)
{
m_component=f;
}
~TShirts() override{}
void show() override
{
m_component->show();
std::cout<<"穿T恤"<<std::endl;
}
private:
Finery::ptr m_component;
};
class Suits:public Finery
{
public:
using ptr=std::shared_ptr<Suits>;
Suits(Finery::ptr f)
{
m_component=f;
}
~Suits(){}
void show() override
{
m_component->show();
std::cout<<"穿西服"<<std::endl;
}
private:
Finery::ptr m_component;
};
int main(int argc, char** argv)
{
Person::ptr person(new Person("Daniel"));
TShirts::ptr shirt(new TShirts(person));
Suits::ptr suit(new Suits(shirt));
suit->show();
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。