Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2565877
  • 博文数量: 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 14:28:20

 vistor.h

点击(此处)折叠或打开

  1. /*
  2.  * vistor.h
  3.  * brief 我的初步理解:定义了一个访问者去访问各个不同职能的类;去获取他们的信息,加以修改后去显示信息!这就是
  4.  * 我的目的。这样可以不同动原先拥有的代码。只需要加以包装就可以完成改造!不知的对不对,等以后水平高了,就...
  5.  * Created on: Sep 25, 2012
  6.  * Author: hl
  7.  */

  8. #ifndef _VISTOR_H_
  9. #define _VISTOR_H_
  10. #include <string>
  11. using namespace std;

  12. /**
  13.  * @brief 报表
  14.  */
  15. class Form
  16. {
  17. public:
  18.     virtual string baseInfo() = 0;
  19. };

  20. /**
  21.  * @brief 普通员工类
  22.  */
  23. class Employee : public Form
  24. {
  25. public:
  26.     string baseInfo();
  27. };

  28. /**
  29.  * @brief 经理类
  30.  */
  31. class Manager
  32. {
  33. public:
  34.     string baseInfo();
  35. };

  36. /**
  37.  * @brief 访问者
  38.  */
  39. class Vistor
  40. {
  41. public:
  42.     Vistor(Employee * pEmployee)
  43.     {
  44.         info = pEmployee->baseInfo() + "\n我是普通员工";
  45.     }
  46.     Vistor(Manager * pManager)
  47.     {
  48.         info = pManager->baseInfo() + "\n我是经理";
  49.     }
  50. public:
  51.     string info;
  52. };

  53. /**
  54.  * @brief 显示报表
  55.  */
  56. class ShowVistor
  57. {
  58. public:
  59.     void Export(Vistor * vis);
  60. };


  61. #endif /* _VISTOR_H_ */
vistor.cpp

点击(此处)折叠或打开

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

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


  11. string Employee::baseInfo()
  12. {
  13.     return "员工号: 120\n工资: 1200";
  14. }

  15. string Manager::baseInfo()
  16. {
  17.     return "员工号: 110\n工资: 5000";
  18. }

  19. void ShowVistor::Export(Vistor * vis)
  20. {
  21.     cout << vis->info << endl;
  22. }

  23. int main()
  24. {
  25.     ShowVistor show;
  26.     show.Export(new Vistor(new Employee()));
  27.     show.Export(new Vistor(new Manager()));

  28.     return 0;
  29. }

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