Chinaunix首页 | 论坛 | 博客
  • 博客访问: 166776
  • 博文数量: 71
  • 博客积分: 165
  • 博客等级: 入伍新兵
  • 技术积分: 431
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-17 00:35
文章分类
文章存档

2013年(3)

2012年(68)

分类:

2012-11-09 13:22:25

responsibility.h

点击(此处)折叠或打开

  1. /*
  2.  * responsibility.h
  3.  *
  4.  * Created on: Sep 29, 2012
  5.  * Author: hl
  6.  */

  7. #ifndef _RESPONSIBILITY_H_
  8. #define _RESPONSIBILITY_H_
  9. #include <string>
  10. using namespace std;

  11. class IWomen
  12. {
  13. public:
  14.     virtual int getType() = 0;
  15.     virtual string getRequest() = 0;
  16. };

  17. class Women : public IWomen
  18. {
  19. public:
  20.     Women(int type, string req);
  21. public:
  22.     int getType();
  23.     string getRequest();
  24. private:
  25.     int _type;
  26.     string _req;
  27. };

  28. /**
  29.  * @责任链处理 - 实现最好都放到.cpp中,不给别人看见!这里我方便学习...
  30.  */
  31. class Handle
  32. {
  33. public:
  34.     Handle(int level)
  35.     {
  36.         this->_level = level;
  37.     }
  38.     ~Handle()
  39.     {
  40.         if (_pHandle)
  41.             delete _pHandle;
  42.     }
  43. public:
  44.     void HandleMessage(IWomen * pIWomen)
  45.     {
  46.         if (this->_level == pIWomen->getType())
  47.             this->response(pIWomen);
  48.         else
  49.         {
  50.             if (this->_pHandle != NULL)
  51.                 this->_pHandle->HandleMessage(pIWomen);
  52.         }
  53.     }
  54. public:
  55.     void setNext(Handle * pHandle)
  56.     {
  57.         this->_pHandle = pHandle;
  58.     }
  59. public:
  60.     virtual void response(IWomen * pIWomen) = 0;
  61. private:
  62.     int _level;
  63.     Handle * _pHandle;
  64. };

  65. /**
  66.  * 父亲响应女儿(Women)的请求
  67.  */
  68. class Father : public Handle
  69. {
  70. public:
  71.     Father() : Handle(1)    /// < 基类初始化变量,解决基类和子类的构造函数参数不同问题
  72.     {

  73.     }
  74.     void response(IWomen * pIWomen);
  75. };

  76. /**
  77.  * 父亲响应女儿(Women)的请求
  78.  */
  79. class Son : public Handle
  80. {
  81. public:
  82.     Son() : Handle(2)    /// < 基类初始化变量,解决基类和子类的构造函数参数不同问题
  83.     {

  84.     }
  85.     void response(IWomen * pIWomen);
  86. };

  87. #endif /* _RESPONSIBILITY_H_ */
responsibility.cpp

点击(此处)折叠或打开

  1. //============================================================================
  2. // Name : responsibility.cpp
  3. // Author : hl
  4. // Version :
  5. // Copyright : Copyright (c) 2012 Tiros
  6. // Description : Responsibility Patternin C++, Ansi-style
  7. //============================================================================

  8. #include "responsibility.h"
  9. #include <iostream>
  10. using namespace std;

  11. Women::Women(int type, string req)
  12. {
  13.     this->_type = type;
  14.     switch (this->_type)
  15.     {
  16.     case 1:
  17.         this->_req = "女儿的请求: " + req;
  18.         break;
  19.     case 2:
  20.         this->_req = "母亲的要求: " + req;
  21.         break;
  22.     case 3:
  23.         break;
  24.     default:
  25.         break;
  26.     }
  27. }

  28. int Women::getType()
  29. {
  30.     return this->_type;
  31. }


  32. string Women::getRequest()
  33. {
  34.     return this->_req;
  35. }


  36. void Father::response(IWomen * pIWomen)
  37. {
  38.     cout << "女儿向父亲请求!" << endl;
  39.     cout << pIWomen->getRequest() << endl;
  40.     cout << "同意" << endl;
  41. }

  42. void Son::response(IWomen * pIWomen)
  43. {
  44.     cout << "母亲向儿子要求!" << endl;
  45.     cout << pIWomen->getRequest() << endl;
  46.     cout << "同意" << endl;
  47. }


  48. int main()
  49. {
  50.     IWomen * wom1 = new Women(1, "父亲大人,我要出去happy");
  51.     IWomen * wom2 = new Women(2, "儿子,我们去吃肯德基");

  52.     Father fath;
  53.     Son son;
  54.     fath.setNext(&son);

  55.     fath.HandleMessage(wom1);
  56.     fath.HandleMessage(wom2);

  57.     return 0;
  58. }

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