Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2570220
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: C/C++

2012-09-30 13:49:09

strtegy.h 

点击(此处)折叠或打开

  1. #ifndef _STRTEGY_H_
  2. #define _STRTEGY_H_
  3. /**
  4. * @brief 锦囊妙计(抽象类)
  5. */
  6. class Strategy
  7. {
  8. public:
  9. virtual void operate() = 0;
  10. };
  11. /**
  12. * @brief 开后门(找乔国老帮忙,免杀刘备)
  13. */
  14. class Backdoor : public Strategy
  15. {
  16. public:
  17. void operate();
  18. };
  19. /**
  20. * @brief 开绿灯(找吴国太帮忙,放行)
  21. */
  22. class Greelight : public Strategy
  23. {
  24. public:
  25. void operate();
  26. };
  27. /**
  28. * @brief 断后(孙夫人断后)
  29. */
  30. class BlockEnemy : public Strategy
  31. {
  32. public:
  33. void operate();
  34. };
  35. /**
  36. * 锦囊(存放妙计)
  37. */
  38. class Content
  39. {
  40. public:
  41. Content()
  42. {
  43. }
  44. Content(Strategy * stra)
  45. {
  46. strat = stra;
  47. }
  48. ~Content()
  49. {
  50. delete strat;
  51. }
  52. public:
  53. void operate()
  54. {
  55. strat->operate();
  56. }
  57. private:
  58. Strategy * strat;
  59. };
  60. #endif // _STRTEGY_H_
strtegy.cpp

点击(此处)折叠或打开

  1. #include "strtegy.h"
  2. #include <iostream>
  3. using namespace std;

  4. void Backdoor::operate()
  5. {
  6.     cout << "找乔国老帮忙,开后门!" << endl;
  7. }


  8. void Greelight::operate()
  9. {
  10.     cout << "找吴国太开绿灯放行!" << endl;
  11. }

  12. void BlockEnemy::operate()
  13. {
  14.     cout << "孙夫人断后!" << endl;
  15. }
main.cpp

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include "strtegy.h"

  3. using namespace std;

  4. /**
  5.  * @brief 模板锦囊(放妙计)
  6.  */
  7. template <typename T>
  8. class ContentT
  9. {
  10. public:
  11.     void operate()
  12.     {
  13.        strat.operate();
  14.     }
  15. private:
  16.     T strat;
  17. };

  18. int main()
  19. {
  20. #if 1
  21.     Content * cont; /// < 得到锦囊
  22.     
  23.     cont = new Content(new Backdoor());
  24.     cont->operate(); /// < 开后门
  25.     delete cont;

  26.     cont = new Content(new Greelight());
  27.     cont->operate(); /// < 开绿灯
  28.     delete cont;

  29.     cont = new Content(new BlockEnemy());
  30.     cont->operate(); /// < 段后
  31.     delete cont;
  32. #endif

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