Chain of Responsibility -- 责任链模式

来源:互联网 发布:mac app store密码错误 编辑:程序博客网 时间:2024/05/22 02:16
 
分类: 设计模式 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元的情况,那么我们通过责任链模式来实现这一场景吧。
那么先做一个表格
级别权限组长500元部门经理5000元公司副经理50000元公司经理500000元
[cpp] view plaincopy
  1. <pre name="code" class="cpp">#include <iostream>  
  2.   
  3. class Request  
  4. {  
  5.     public:  
  6.         Request(double m)  
  7.         :money(m)  
  8.         {};  
  9.         double getMoney(){return money;}  
  10.     private:  
  11.         double money;    
  12. };  
  13.   
  14. class IHandle  
  15. {  
  16.     public:  
  17.         IHandle(IHandle *p)  
  18.         :nextHandle(p)  
  19.         {}  
  20.         virtual bool IsAuthority(Request r)=0;  
  21.         virtual void doHandle(Request r)=0;  
  22.     protected:  
  23.         IHandle * nextHandle;  
  24.       
  25. };  
  26.   
  27. class headmanHandle:public IHandle  
  28. {  
  29.     public:  
  30.         headmanHandle(IHandle *p)  
  31.         :IHandle(p)  
  32.         {}  
  33.         bool IsAuthority(Request r);  
  34.         void doHandle(Request r);  
  35. };  
  36. bool headmanHandle::IsAuthority(Request r)  
  37. {  
  38.     if(r.getMoney()>500)return false;  
  39.     else return true;  
  40. }  
  41. void headmanHandle::doHandle(Request r)  
  42. {  
  43.     if(IsAuthority(r))  
  44.     {  
  45.         std::cout<<"The headman do the request,which the request money is"<<r.getMoney()<<std::endl;  
  46.     }  
  47.     else  
  48.     {  
  49.         nextHandle->doHandle(r);  
  50.     }  
  51. }  
  52.   
  53.   
  54. class DepartmentMHandle:public IHandle  
  55. {  
  56.     public:  
  57.         DepartmentMHandle(IHandle *p)  
  58.         :IHandle(p)  
  59.         {}  
  60.         bool IsAuthority(Request r);  
  61.         void doHandle(Request r);  
  62. };  
  63. bool DepartmentMHandle::IsAuthority(Request r)  
  64. {  
  65.     if(r.getMoney()>5000)return false;  
  66.     else return true;  
  67. }  
  68. void DepartmentMHandle::doHandle(Request r)  
  69. {  
  70.     if(IsAuthority(r))  
  71.     {  
  72.         std::cout<<"The department manager do the request,which the request money is"<<r.getMoney()<<std::endl;  
  73.     }  
  74.     else  
  75.     {  
  76.         nextHandle->doHandle(r);  
  77.     }  
  78. }  
  79.   
  80.   
  81. class CompanyDMHandle:public IHandle  
  82. {  
  83.     public:  
  84.         CompanyDMHandle(IHandle *p)  
  85.         :IHandle(p)  
  86.         {}  
  87.         bool IsAuthority(Request r);  
  88.         void doHandle(Request r);  
  89. };  
  90. bool CompanyDMHandle::IsAuthority(Request r)  
  91. {  
  92.     if(r.getMoney()>50000)return false;  
  93.     else return true;  
  94. }  
  95. void CompanyDMHandle::doHandle(Request r)  
  96. {  
  97.     if(IsAuthority(r))  
  98.     {  
  99.         std::cout<<"The company deputy manager do the request,which the request money is"<<r.getMoney()<<std::endl;  
  100.     }  
  101.     else  
  102.     {  
  103.         nextHandle->doHandle(r);  
  104.     }  
  105. }  
  106.   
  107.   
  108. class CompanyMHandle:public IHandle  
  109. {  
  110.     public:  
  111.         CompanyMHandle(IHandle *p)  
  112.         :IHandle(p)  
  113.         {}  
  114.         bool IsAuthority(Request r);  
  115.         void doHandle(Request r);  
  116. };  
  117. bool CompanyMHandle::IsAuthority(Request r)  
  118. {  
  119.     if(r.getMoney()>500000)return false;  
  120.     else return true;  
  121. }  
  122. void CompanyMHandle::doHandle(Request r)  
  123. {  
  124.     if(IsAuthority(r))  
  125.     {  
  126.         std::cout<<"The company manager do the request,which the request money is "<<r.getMoney()<<std::endl;  
  127.     }  
  128.     else  
  129.     {  
  130.         std::cout<<"You are the boss of thie company!Because you use up"<<r.getMoney()<<std::endl;  
  131.     }  
  132. }  
  133.   
  134. int main(int argc,char ** argv)  
  135. {  
  136.     IHandle *p4=new CompanyMHandle(NULL);  
  137.     IHandle *p3=new CompanyDMHandle(p4);  
  138.     IHandle *p2=new DepartmentMHandle(p3);  
  139.     IHandle *p1=new headmanHandle(p2);  
  140.     Request r1(300);  
  141.     Request r2(3000);  
  142.     Request r3(30000);  
  143.     Request r4(300000);  
  144.     Request r5(3000000);  
  145.     p1->doHandle(r1);  
  146.     p1->doHandle(r2);  
  147.     p1->doHandle(r3);  
  148.     p1->doHandle(r4);  
  149.     p1->doHandle(r5);  
  150.     delete p1;  
  151.     delete p2;  
  152.     delete p3;  
  153.     delete p4;  
  154.     return 0;  
  155. }</pre><br>  
  156. <br>  
  157. <pre></pre>  
  158. <span style="line-height:26px"><span style="font-size:16px">运行结果为</span></span><br>