《職責鏈模式》

職責鏈模式,用我的理解就是動作的處理鏈表。根據請求的不同類型,在鏈表查找相應類型的處理者。處理者是一個鏈表。

用下面的一個圖來解釋

 

基本的UML圖爲

  1. // ChainofResponsibility.cpp : 定義控制檯應用程序的入口點。  
  2. //************************************************************************/      
  3. /* @filename    ChainofResponsibility.cpp 
  4. @author       wallwind   
  5. @createtime    2012/11/6 11:58 
  6. @function     責任鏈模式 
  7. @email       [email protected]   
  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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章