Chinaunix首页 | 论坛 | 博客
  • 博客访问: 169357
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 638
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 10:59
个人简介

喜欢coding,因为那是一件伟大的事情,是将无生命的IC赋予灵魂的过程,让我拥有了和上帝一样的成就感。(w1c2g3@163.com)

文章分类

全部博文(60)

文章存档

2017年(7)

2016年(41)

2015年(1)

2014年(4)

2013年(7)

我的朋友

分类: C/C++

2016-10-30 23:19:47

访问者模式 当你想要为一个对象的组合增加新的能力,且封装并不重要时,就使用访问者模式(Visitor Pattern)




  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4.   
  5. using namespace std;

  6. class CPerson;
  7. class CStudent;
  8. class CTeacher;
  9.   
  10. class CVisitor;
  11. class CPrinter;


  12. class CPerson {
  13. protected:
  14.     string name;
  15.     int gender;
  16.     CPerson() {}
  17. public:
  18.     virtual void Accept(CVisitor &) {}
  19.     void SetName(const string& Name) {
  20.         name = Name;
  21.     }
  22.     string GetName() const {
  23.         return name;
  24.     }
  25.     void SetGender(const int &Gender) {
  26.         gender = Gender;
  27.     }
  28.     int GetGender() const {
  29.         return gender;
  30.     }
  31. };

  32. class CVisitor {
  33. public:
  34.     virtual void Visit(CStudent &) = 0;
  35.     virtual void Visit(CTeacher &) = 0;
  36. };

  37. class CStudent : public CPerson {
  38. private:
  39.     int grade;
  40. public:
  41.     CStudent(string Name,int Gender,int Grade) {
  42.         name = Name;
  43.         gender = Gender;
  44.         grade = Grade;
  45.     }
  46. public:
  47.     virtual void Accept(CVisitor &printer) {
  48.         printer.Visit(*this);
  49.     }
  50.     void SetGrade(int Grade) {
  51.         grade = Grade;
  52.     }
  53.     int GetGrade() const {
  54.         return grade;
  55.     }
  56. };

  57. class CTeacher:public CPerson {
  58. private:
  59.     int service_time;
  60. public:
  61.     CTeacher(string Name, int Grade, int ServiceTime) {
  62.         name = Name;
  63.         gender = Grade;
  64.         service_time = ServiceTime;
  65.     }
  66.     virtual void Accept(CVisitor& printer) {
  67.         printer.Visit(*this);
  68.     }
  69.     void SetServiceTime(int ServiceTime) {
  70.         service_time = ServiceTime;
  71.     }
  72.     int GetServiceTime() const {
  73.         return service_time;
  74.     }
  75. };
  76.   
  77. class CPrinter: public CVisitor {
  78. public:
  79.     void Visit(CStudent &s) {
  80.         cout << "student:" <<endl;
  81.         cout << "\t Name:" <<s.GetName() <<endl;
  82.         cout << "\t Gender:" <<
  83.             (s.GetGender() ? "male" : "female") << endl;
  84.         cout << "\t Grade:" <<s.GetGrade() <<endl;
  85.     }
  86.   
  87.     void Visit(CTeacher &t) {
  88.         cout << "Teacher:" <<endl;
  89.         cout << "\t Name:" <<t.GetName() <<endl;
  90.         cout << "\t Gender:" <<
  91.             (t.GetGender() ? "male" : "female") << endl;
  92.         cout << "\t Service Time:" << t.GetServiceTime() << endl;
  93.     }
  94. };

  95. class Organization {
  96. private:
  97.     typedef list<CPerson*> CMemberList;
  98.     CMemberList member_list;

  99. public:
  100.     void Add(CPerson* person) {
  101.         member_list.push_back(person);
  102.     }
  103.     void PrintMembers(CPrinter &printer) {
  104.         CMemberList::iterator itr = member_list.begin();
  105.         for(; itr != member_list.end(); ++itr) {
  106.             (*itr)->Accept(printer);
  107.         }
  108.     }
  109.     ~Organization() {
  110.         CMemberList::iterator itr = member_list.begin();
  111.         for(; itr != member_list.end(); ++itr) {
  112.             delete *itr;
  113.         }
  114.     }
  115. };
  116.   

  117. int main(int argc, char **argv)
  118. {
  119.     Organization Planting;
  120.     Planting.Add(new CTeacher("Johnny",1,10));
  121.     Planting.Add(new CStudent("Catherine",0,1));
  122.     Planting.Add(new CStudent("Peter",1,2));
  123.   
  124.     CPrinter printer;
  125.     Planting.PrintMembers(printer);
  126.   
  127.     return 0;
  128. }

阅读(963) | 评论(0) | 转发(0) |
0

上一篇:原型模式

下一篇:中介者模式

给主人留下些什么吧!~~