Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2570185
  • 博文数量: 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)

分类: 项目管理

2012-09-30 13:58:10

decorator.h

点击(此处)折叠或打开

  1. /*
  2.  * decorator.h
  3.  *
  4.  * Created on: Sep 21, 2012
  5.  * Author: hl
  6.  * 又是虚函数的力量,调用方法和接口不变,只需要改变调用对象!
  7.  */

  8. #ifndef _DECORATOR_H_
  9. #define _DECORATOR_H_
  10. #include <iostream>
  11. using namespace std;

  12. /**
  13.  * @brief 成绩单
  14.  */
  15. class SchoolReport
  16. {
  17. public:
  18.     virtual void reaport() = 0;
  19.     virtual void sign(string name) = 0;
  20. };

  21. /**
  22.  * @brief 第四学期成绩单
  23.  */
  24. class FouthGradeSchoolReport : public SchoolReport
  25. {
  26. public:
  27.     void reaport();
  28.     void sign(string name);
  29. };

  30. /**
  31.  * @brief 装饰器 - 包装下成绩单,增加排名之类的!
  32.  */
  33. class Decorator : public SchoolReport
  34. {
  35. public:
  36.     void reaport()
  37.     {
  38.         fouth.reaport();
  39.         rank();
  40.     }
  41.     void rank()
  42.     {
  43.         cout << "排名 32" << endl;
  44.     }
  45.     void sign(string name)
  46.     {
  47.         fouth.sign(name);
  48.     }
  49. private:
  50.     FouthGradeSchoolReport fouth;
  51. };

  52. #endif /* _DECORATOR_H_ */
decorator.cpp

点击(此处)折叠或打开

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

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

  11. void FouthGradeSchoolReport::reaport()
  12. {
  13.     cout << "语文 45 数学 54" << endl;
  14. }

  15. void FouthGradeSchoolReport::sign(string name)
  16. {
  17.     cout << "家长请签名: " + name << endl;
  18. }

  19. int main()
  20. {
  21.     SchoolReport * pSchoolReport = new Decorator();
  22.     pSchoolReport->reaport();
  23.     /// < 家长一看排名还不错,就签名了!(作者说)
  24.     pSchoolReport->sign("小五");

  25.     return 0;
  26. }

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