Chain of Responsibility -- 责任链模式
来源:互联网 发布:mac app store密码错误 编辑:程序博客网 时间:2024/05/22 02:16
分类: 设计模式2013-01-14 09:47 123人阅读 评论(2) 收藏 举报
设计模式
在软件构建构成中,一个请求可能被多个对象处理,但是每个请求在运行时只能有一个接收者,如果显示指定,将必不可少地带来请求发送者与接收者的紧密耦合。COR(Chain of Reposibility)设计模式可以使请求的发送者不需要指定具体的接收者,让请求的接收者自己在运行时决定来处理请求,从而使两者解耦。
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象对应它为止。
“Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.” - GoF
实例解析
例如就拿我们工作中的事情来说,就拿报销来说,一个组长的审批权力为500元及其以下,他都可以处理这些报销请求,当超过500元时候,他就没有权利审批了,就需要到部门经理那里了,经理可以审批5000元及其以下,如果超过5000元就需要找公司副经理了,他可以审批50000元及其以下了,当超过50000元,就需要找公司经理来审批了,这里假定是500000元,好吧你如果说超过了5000000元,怎么办?我想国内还没有公司愿意为你报500000元,再说你自己也不愿意拿500000元出来先垫着。好了这里不考虑超过500000元的情况,那么我们通过责任链模式来实现这一场景吧。
那么先做一个表格
- <pre name="code" class="cpp">#include <iostream>
- class Request
- {
- public:
- Request(double m)
- :money(m)
- {};
- double getMoney(){return money;}
- private:
- double money;
- };
- class IHandle
- {
- public:
- IHandle(IHandle *p)
- :nextHandle(p)
- {}
- virtual bool IsAuthority(Request r)=0;
- virtual void doHandle(Request r)=0;
- protected:
- IHandle * nextHandle;
- };
- class headmanHandle:public IHandle
- {
- public:
- headmanHandle(IHandle *p)
- :IHandle(p)
- {}
- bool IsAuthority(Request r);
- void doHandle(Request r);
- };
- bool headmanHandle::IsAuthority(Request r)
- {
- if(r.getMoney()>500)return false;
- else return true;
- }
- void headmanHandle::doHandle(Request r)
- {
- if(IsAuthority(r))
- {
- std::cout<<"The headman do the request,which the request money is"<<r.getMoney()<<std::endl;
- }
- else
- {
- nextHandle->doHandle(r);
- }
- }
- class DepartmentMHandle:public IHandle
- {
- public:
- DepartmentMHandle(IHandle *p)
- :IHandle(p)
- {}
- bool IsAuthority(Request r);
- void doHandle(Request r);
- };
- bool DepartmentMHandle::IsAuthority(Request r)
- {
- if(r.getMoney()>5000)return false;
- else return true;
- }
- void DepartmentMHandle::doHandle(Request r)
- {
- if(IsAuthority(r))
- {
- std::cout<<"The department manager do the request,which the request money is"<<r.getMoney()<<std::endl;
- }
- else
- {
- nextHandle->doHandle(r);
- }
- }
- class CompanyDMHandle:public IHandle
- {
- public:
- CompanyDMHandle(IHandle *p)
- :IHandle(p)
- {}
- bool IsAuthority(Request r);
- void doHandle(Request r);
- };
- bool CompanyDMHandle::IsAuthority(Request r)
- {
- if(r.getMoney()>50000)return false;
- else return true;
- }
- void CompanyDMHandle::doHandle(Request r)
- {
- if(IsAuthority(r))
- {
- std::cout<<"The company deputy manager do the request,which the request money is"<<r.getMoney()<<std::endl;
- }
- else
- {
- nextHandle->doHandle(r);
- }
- }
- class CompanyMHandle:public IHandle
- {
- public:
- CompanyMHandle(IHandle *p)
- :IHandle(p)
- {}
- bool IsAuthority(Request r);
- void doHandle(Request r);
- };
- bool CompanyMHandle::IsAuthority(Request r)
- {
- if(r.getMoney()>500000)return false;
- else return true;
- }
- void CompanyMHandle::doHandle(Request r)
- {
- if(IsAuthority(r))
- {
- std::cout<<"The company manager do the request,which the request money is "<<r.getMoney()<<std::endl;
- }
- else
- {
- std::cout<<"You are the boss of thie company!Because you use up"<<r.getMoney()<<std::endl;
- }
- }
- int main(int argc,char ** argv)
- {
- IHandle *p4=new CompanyMHandle(NULL);
- IHandle *p3=new CompanyDMHandle(p4);
- IHandle *p2=new DepartmentMHandle(p3);
- IHandle *p1=new headmanHandle(p2);
- Request r1(300);
- Request r2(3000);
- Request r3(30000);
- Request r4(300000);
- Request r5(3000000);
- p1->doHandle(r1);
- p1->doHandle(r2);
- p1->doHandle(r3);
- p1->doHandle(r4);
- p1->doHandle(r5);
- delete p1;
- delete p2;
- delete p3;
- delete p4;
- return 0;
- }</pre><br>
- <br>
- <pre></pre>
- <span style="line-height:26px"><span style="font-size:16px">运行结果为</span></span><br>
- 责任链(Chain of Responsibility)模式
- 责任链模式(Chain of Responsibility Pattern)
- 责任链模式(Chain of Responsibility Pattern)
- Chain of Responsibility 责任链模式
- 责任链模式 chain of responsibility
- 责任链模式(chain of responsibility)
- 责任链模式(Chain of Responsibility)
- 责任链模式(Chain of Responsibility)
- 责任链模式【CHAIN OF RESPONSIBILITY PATTERN 】
- 责任链模式【Chain of Responsibility Pattern】
- Chain of Responsibility -- 责任链模式
- 责任链模式(Chain of Responsibility Pattern)
- 责任链(chain of responsibility)模式
- Chain of Responsibility(责任链模式)
- 责任链模式(Chain of Responsibility Pattern)
- 责任链模式(Chain of Responsibility)
- Java责任链模式(Chain of Responsibility)
- 责任链模式【Chain of Responsibility Pattern】
- el表达式的判断
- linux命令
- linux简介
- 中文拼音库
- openfire exe文件安装服务器方法
- Chain of Responsibility -- 责任链模式
- 在Android上使用LocalSocket实现上层Java和底层C++的通信
- linux中的dns
- sql server数字型字段,有非法字符的修复方法
- 类似百度文库阅读器功能出现问题总结
- 80x86标志寄存器图解
- WPF Control 控件介绍【附Demo】
- 产品经理的工具
- 五种网络模型流程(自我理解篇)