Chinaunix首页 | 论坛 | 博客
  • 博客访问: 959726
  • 博文数量: 77
  • 博客积分: 821
  • 博客等级: 军士长
  • 技术积分: 1905
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-23 16:17
个人简介

学校:上海交通大学软件工程 学历:硕士 行业:从事流媒体移动开发 QQ: 412595942 邮箱:yiikai1987910@gmail.com

文章分类

全部博文(77)

文章存档

2016年(4)

2015年(15)

2014年(16)

2013年(12)

2012年(21)

2011年(9)

分类: C/C++

2015-05-06 16:15:40

    责任链的原理google多的事
   直接上自己的理解和类图代码:
   

代码:

点击(此处)折叠或打开

  1. // LineTree.cpp : Defines the entry point for the console application.
  2. //责任链模式
  3. //

  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <list>

  7. class Handler
  8. {
  9. public:
  10.     Handler(int seriNum){
  11.         m_num = seriNum;
  12.     }
  13.     virtual ~Handler(){}

  14.     virtual void handleRequest(int seriNum) = 0;
  15. public:
  16.     void setNextHandler(Handler *tmp)
  17.     {
  18.         m_nextHandler = tmp;
  19.     }
  20. protected:
  21.     Handler* getNextHandler()
  22.     {
  23.         return m_nextHandler;
  24.     }
  25. protected:
  26.     int m_num{ 0 };
  27. private:
  28.     Handler *m_nextHandler{nullptr};
  29.     
  30. };

  31. class firstHandler : public Handler{
  32. public:
  33.     firstHandler(int seriNum) : Handler(seriNum)
  34.     {

  35.     }
  36.     ~firstHandler()
  37.     {

  38.     }
  39.     virtual void handleRequest(int seriNum)
  40.     {
  41.         if (seriNum == m_num)
  42.         {
  43.             std::cout << "first handle" << std::endl;
  44.         }
  45.         else
  46.         {
  47.             Handler* next = getNextHandler();
  48.             if (next == nullptr)
  49.             {
  50.                 std::cout << "It's over" << std::endl;
  51.                 return;
  52.             }
  53.             next->handleRequest(seriNum);
  54.         }
  55.     }
  56. };

  57. class secondHandler : public Handler{
  58. public:
  59.     secondHandler(int seriNum) : Handler(seriNum)
  60.     {

  61.     }
  62.     ~secondHandler()
  63.     {

  64.     }
  65.     virtual void handleRequest(int seriNum)
  66.     {
  67.         if (seriNum == m_num)
  68.         {
  69.             std::cout << "second handle" << std::endl;
  70.         }
  71.         else
  72.         {
  73.             Handler* next = getNextHandler();
  74.             if (next == nullptr)
  75.             {
  76.                 std::cout << "It's over" << std::endl;
  77.                 return;
  78.             }
  79.             next->handleRequest(seriNum);
  80.         }
  81.     }
  82. };


  83. class ThirdHandler : public Handler{
  84. public:
  85.     ThirdHandler(int seriNum) : Handler(seriNum)
  86.     {

  87.     }
  88.     ~ThirdHandler()
  89.     {

  90.     }
  91.     virtual void handleRequest(int seriNum)
  92.     {
  93.         if (seriNum == m_num)
  94.         {
  95.             std::cout << "Third handle" << std::endl;
  96.         }
  97.         else
  98.         {
  99.             Handler* next = getNextHandler();
  100.             if (next == nullptr)
  101.             {
  102.                 std::cout << "It's over" << std::endl;
  103.                 return;
  104.             }
  105.             next->handleRequest(seriNum);
  106.         }
  107.     }
  108. };

  109. int _tmain(int argc, _TCHAR* argv[])
  110. {
  111.     
  112.     //先创建三个具体的处理类
  113.     Handler *first = new firstHandler(1);
  114.     Handler *second = new secondHandler(2);
  115.     Handler *third = new ThirdHandler(3);

  116.     first->setNextHandler(second);
  117.     second->setNextHandler(third);
  118.     third->setNextHandler(nullptr);

  119.     Handler *startHandler = first;
  120.     startHandler->handleRequest(1);
  121.     return 0;
  122. }


阅读(2531) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~