1 Star 0 Fork 0

Tzewa Lam/DesignPatternsInCpp

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 768 Bytes
一键复制 编辑 原始数据 按行查看 历史
Tzewa Lam 提交于 2024-07-24 16:59 +08:00 . added 代理模式
#include <iostream>
#include <memory>
class House {
public:
House(int area = 0) : m_area(area) {}
virtual ~House() = default;
virtual bool isBig() const = 0;
protected:
int m_area;
};
class RealHouse : public House {
public:
RealHouse(int area) : House(area) {}
bool isBig() const override {
return m_area > 100;
}
};
class Proxy : House {
public:
Proxy(int size) {
m_house.reset(new RealHouse(size));
}
bool isBig() const override {
return m_house->isBig();
}
private:
std::unique_ptr<RealHouse> m_house;
};
int main() {
int n;
std::cin >> n;
while (n --) {
int size;
std::cin >> size;
Proxy house(size);
if (house.isBig()) {
std::cout << "YES" << std::endl;
} else {
std::cout << "NO" << std::endl;
}
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fingsinz/design-patterns-in-cpp.git
git@gitee.com:fingsinz/design-patterns-in-cpp.git
fingsinz
design-patterns-in-cpp
DesignPatternsInCpp
master

搜索帮助