加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
proxy.cpp 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
Daniel_Du 提交于 2022-02-16 11:10 . 20220216 11:10
//代理模式
#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;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化