1 Star 0 Fork 0

mktime/design-pattern-cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
adapter.cpp 931 Bytes
一键复制 编辑 原始数据 按行查看 历史
#include <iostream>
using namespace std;
class target {
public:
target() {
cout << "target::target()" << endl;
}
~target() {
cout << "target::~target()" << endl;
}
virtual void request() = 0;
};
class adaptee {
public:
adaptee() {
cout << "adaptee::adaptee()" << endl;
}
~adaptee() {
cout << "adaptee::~adaptee()" << endl;
}
void specialRequest() {
cout << "specialRequest" << endl;
}
};
class adapter: public target {
private:
adaptee* m_adaptee;
public:
adapter(adaptee* a) {
this->m_adaptee = a;
}
~adapter() {
cout << "adapter::~adapter() called" << endl;
if (m_adaptee) {
delete m_adaptee;
}
}
void request() {
m_adaptee->specialRequest();
}
};
int main(int argc, char** argv) {
target* t = new adapter(new adaptee());
t->request();
delete t;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mktime/design-pattern-cpp.git
git@gitee.com:mktime/design-pattern-cpp.git
mktime
design-pattern-cpp
design-pattern-cpp
master

搜索帮助