《职责链模式》

职责链模式,用我的理解就是动作的处理链表。根据请求的不同类型,在链表查找相应类型的处理者。处理者是一个链表。

用下面的一个图来解释

 

基本的UML图为

[cpp]  view plain copy
  1. // ChainofResponsibility.cpp : 定义控制台应用程序的入口点。  
  2. //************************************************************************/      
  3. /* @filename    ChainofResponsibility.cpp 
  4. @author       wallwind   
  5. @createtime    2012/11/6 11:58 
  6. @function     责任链模式 
  7. @email       wochenglin@qq.com   
  8. @weibo      @成林有理想 
  9. */      
  10. /************************************************************************/  
  11.   
  12. #include "stdafx.h"  
  13. #include <iostream>  
  14.   
  15. using namespace std;  
  16.   
  17. class IChain  
  18. {  
  19. public:  
  20.     IChain():m_nextChain(NULL)  
  21.     {  
  22.   
  23.     }  
  24.     virtual ~IChain()  
  25.     {  
  26.         if (m_nextChain !=NULL)  
  27.         {  
  28.             delete m_nextChain;  
  29.             m_nextChain = NULL;  
  30.         }  
  31.     }  
  32.   
  33.     void setChainHandler(IChain* handler)  
  34.     {  
  35.         m_nextChain = handler;  
  36.     }  
  37.     virtual void handleReq(int reqtype) = 0;  
  38.   
  39.       
  40. protected:  
  41.     IChain* m_nextChain;  
  42.       
  43. };  
  44.   
  45. class FirstHandler:public IChain  
  46. {  
  47. public:  
  48.     FirstHandler():IChain()  
  49.     {  
  50.     }  
  51.     virtual ~FirstHandler(){}  
  52.   
  53.     virtual void handleReq(int reqtype)  
  54.     {  
  55.         if (reqtype <3)  
  56.         {  
  57.             cout<<"FirstHandler::handleReq"<<endl;  
  58.         }  
  59.         else if (m_nextChain !=NULL)  
  60.         {  
  61.             m_nextChain->handleReq(reqtype);  
  62.         }  
  63.         else  
  64.         {  
  65.             cout<<"no handler"<<endl;  
  66.         }  
  67.     }  
  68. };  
  69.   
  70. class SecondHandler:public IChain  
  71. {  
  72. public:  
  73.     SecondHandler():IChain()  
  74.     {  
  75.           
  76.     }  
  77.     virtual ~SecondHandler(){}  
  78.   
  79.     virtual void handleReq(int reqtype)  
  80.     {  
  81.         if (reqtype <7)  
  82.         {  
  83.             cout<<"SecondHandler::handleReq"<<endl;  
  84.         }  
  85.         else if (m_nextChain !=NULL)  
  86.         {  
  87.             m_nextChain->handleReq(reqtype);  
  88.         }  
  89.         else  
  90.         {  
  91.             cout<<"no handler"<<endl;  
  92.         }  
  93.     }  
  94.   
  95.   
  96.       
  97. };  
  98.   
  99. class ThirdHandler:public IChain  
  100. {  
  101. public:  
  102.     ThirdHandler():IChain()  
  103.     {  
  104.     }  
  105.     virtual ~ThirdHandler(){}  
  106.   
  107.     virtual void handleReq(int reqtype)  
  108.     {  
  109.         if (reqtype <9)  
  110.         {  
  111.             cout<<"ThirdHandler::handleReq"<<endl;  
  112.         }  
  113.         else if (m_nextChain !=NULL)  
  114.         {  
  115.             m_nextChain->handleReq(reqtype);  
  116.         }  
  117.         else  
  118.         {  
  119.             cout<<"no handler"<<endl;  
  120.         }  
  121.     }  
  122.   
  123.   
  124. };  
  125.   
  126. int _tmain(int argc, _TCHAR* argv[])  
  127. {  
  128.   
  129.     IChain *p1handler= new FirstHandler;  
  130.     IChain *p2handler= new SecondHandler;  
  131.     IChain *p3handler= new ThirdHandler;  
  132.   
  133.     p1handler ->setChainHandler(p2handler);  
  134.     p2handler->setChainHandler(p3handler);  
  135.   
  136.     p3handler ->handleReq(4);  
  137.   
  138.     delete p1handler,p2handler,p3handler;  
  139.     return 0;  
  140. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值