1 Star 0 Fork 617

邵小小邵/DesignPattern

forked from Micooz/DesignPattern 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ChainOfResponsibilityPattern.cpp 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
Micooz 提交于 2014-10-09 10:37 . 加入访问者模式,责任链模式
#include <iostream>
using namespace std;
class Handle {
public:
virtual ~Handle() { }
virtual void HandleRequest() = 0;
void SetSuccessor(Handle *succ) {
_succ = succ;
}
Handle* GetSuccessor()const {
return _succ;
}
protected:
Handle() { _succ = nullptr; }
Handle(Handle* succ) {
_succ = succ;
}
private:
Handle* _succ;
};
class ConcreteHandleA :public Handle {
public:
ConcreteHandleA() { }
ConcreteHandleA(Handle* succ)
:Handle(succ) {
}
void HandleRequest() {
if (this->GetSuccessor() != 0) {
cout << "ConcreteHandleA--Successor" << endl;
GetSuccessor()->HandleRequest();
}
else {
cout << "ConcreteHandleA::HandleRequest" << endl;
}
}
};
class ConcreteHandleB :public Handle {
public:
ConcreteHandleB() { }
ConcreteHandleB(Handle* succ)
:Handle(succ) {
}
void HandleRequest() {
if (this->GetSuccessor() != 0) {
cout << "ConcreteHandleB--Successor" << endl;
GetSuccessor()->HandleRequest();
}
else {
cout << "ConcreteHandleB::HandleRequest" << endl;
}
}
};
int main() {
Handle *h1 = new ConcreteHandleA();
Handle *h2 = new ConcreteHandleB(h1);// or h1->SetSuccessor(h2);
h2->HandleRequest();
delete h1;
delete h2;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/shao_xiaoshao/DesignPattern.git
git@gitee.com:shao_xiaoshao/DesignPattern.git
shao_xiaoshao
DesignPattern
DesignPattern
master

搜索帮助