Chain of Responsibility模式
来源:互联网 发布:淘宝售假保证金 编辑:程序博客网 时间:2024/05/22 16:42
Chain of Responsibility模式
熟悉MFC的朋友都知道,MFC是"基于消息,事件驱动"滴,它的向上传递消息的机制跟职责链的思想是相同的,其实Qt中的
touchEvent也是这样处理的。
职责链模式的最大一个优点就是降低了系统的耦合性,请求的发送者完全不知道该请求会被哪个应答对象处理。
C++代码示例:
<strong>//Handle.h</strong>#ifndef _HANDLE_H_#define _HANDLE_H_class Handle{ public: virtual ~Handle(); virtual void HandleRequest() = 0; void SetSuccessor(Handle* succ); Handle* GetSuccessor(); protected: Handle(); Handle(Handle* succ); private: Handle* _succ;};class ConcreteHandleA:public Handle{public: ConcreteHandleA(); ~ConcreteHandleA(); ConcreteHandleA(Handle* succ); void HandleRequest();};class ConcreteHandleB:public Handle{public: ConcreteHandleB(); ~ConcreteHandleB(); ConcreteHandleB(Handle* succ); void HandleRequest();};#endif //~_HANDLE_H_<strong>//Handle.cpp</strong>#include "Handle.h"#include <iostream>using namespace std;Handle::Handle(){ _succ = 0;}Handle::~Handle(){ delete _succ;}Handle::Handle(Handle* succ){ this->_succ = succ;}void Handle::SetSuccessor(Handle* succ){ _succ = succ;}Handle* Handle::GetSuccessor(){ return _succ;}void Handle::HandleRequest(){}ConcreteHandleA::ConcreteHandleA(){}ConcreteHandleA::ConcreteHandleA(Handle*succ):Handle(succ){}ConcreteHandleA::~ConcreteHandleA(){}void ConcreteHandleA::HandleRequest(){ if (this->GetSuccessor() != 0) { cout<<"ConcreteHandleA 我把处理权给后继节点....."<<endl; this->GetSuccessor()->HandleRequest(); } else { cout<<"ConcreteHandleA 没有后继了,我必须自己处理...."<<endl; }}ConcreteHandleB::ConcreteHandleB(){}ConcreteHandleB::ConcreteHandleB(Handle*succ):Handle(succ){}ConcreteHandleB::~ConcreteHandleB(){}void ConcreteHandleB::HandleRequest(){ if (this->GetSuccessor() != 0) { cout<<"ConcreteHandleB 我把处理权给后继节点....."<<endl; this->GetSuccessor()->HandleRequest(); } else { cout<<"ConcreteHandleB 没有后继了,我必须自己处理...."<<endl; }}<strong>//main.cpp</strong>#include "Handle.h"#include <iostream>using namespace std;int main(int argc,char* argv[]){ Handle* h1 = new ConcreteHandleA(); Handle* h2 = new ConcreteHandleB(); h1->SetSuccessor(h2); h1->HandleRequest(); return 0;}
0 0
- Chain of responsibility模式
- Chain of Responsibility模式
- Chain Of Responsibility模式
- Chain Of Responsibility模式
- Chain of Responsibility 模式
- Chain of Responsibility模式
- Chain of Responsibility 模式
- Chain of Responsibility模式
- Chain of Responsibility模式
- Chain of Responsibility模式
- Chain of Responsibility模式
- Chain of Responsibility模式
- 模式中的chain of responsibility
- Chain of Responsibility设计模式
- Chain of Responsibility设计模式
- Chain Of Responsibility-----设计模式
- 设计模式之 Chain of Responsibility 模式
- 设计模式之Chain of Responsibility模式
- 设计模式
- NSString 与 NSMutableString 常用方法
- hust OJ如何写一个special judge
- vb跟access做图书馆管理系统怎么做
- Sublime text3 插件ColorPicker(调色板)不能使用快捷键
- Chain of Responsibility模式
- android 5.0 Material Design酷炫风格的开源项目集合
- 第五章知识导图
- POJ 题目2182 Lost Cows(树状数组+二分)
- AIX PowerPC体系结构及其溢出技术学习笔记
- PHP设计模式(一)
- ArcGIS教程:ArcMap栅格失量化快捷键总结
- ComponentName类讲解
- myeclipse 加载svn插件