代码拉取完成,页面将自动刷新
//代理模式
#include <string>
#include <iostream>
#include <memory>
class Girl
{
public:
Girl(std::string name) : m_name(name) {}
std::string getName() { return m_name; }
void setName(std::string name) { m_name = name; }
~Girl(){};
private:
std::string m_name;
};
class AbsGiveGift
{
public:
virtual void giveDolls() = 0;
virtual void giveFlowers() = 0;
virtual void giveChocolate() = 0;
};
class Persuit : public AbsGiveGift
{
public:
using ptr = std::shared_ptr<Persuit>;
Persuit(Girl girl) : m_girl(girl) {}
~Persuit() {}
void giveDolls() override
{
std::cout << m_girl.getName() << ",送你洋娃娃" << std::endl;
}
void giveFlowers() override
{
std::cout << m_girl.getName() << ",送你玫瑰花" << std::endl;
}
void giveChocolate() override
{
std::cout << m_girl.getName() << ",送你巧克力" << std::endl;
}
private:
Girl m_girl;
};
class Proxy : public AbsGiveGift
{
public:
using ptr = std::shared_ptr<Proxy>;
Proxy(Girl girl) : m_persuit(new Persuit(girl))
{
}
~Proxy() {}
void giveFlowers()
{
m_persuit->giveDolls();
}
void giveDolls()
{
m_persuit->giveFlowers();
}
void giveChocolate()
{
m_persuit->giveChocolate();
}
private:
Persuit::ptr m_persuit;
};
int main(int argc, char** argv)
{
Girl girl("李娇娇");
Proxy::ptr proxy(new Proxy(girl));
proxy->giveDolls();
proxy->giveFlowers();
proxy->giveChocolate();
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。