Design Patterns visitors beginner mode

 

Program Case: I have only two working languages ​​of mathematics this, but anyone can view.

  1  / * *********************************************** ***********************************************
   2  - -------------------- ---------------------- program case
   3  child on school exercise books two types of math homework and Chinese homework in this book, but to write it myself                                  
   4  teacher for marking it, parents should check it out, sometimes it depends on friends and family to a
   5  Overall, this work on two, but it has a lot of access, you may have to expand
   6  
  7  --------------------- --------------- case Study -------
   8  elementary school math and language work only two kinds of data equivalent to fixed; different people to visit it, is equivalent to the operation of the data, this is change
   9  may be the teacher may also be parents, and different people visit it to his operation are different; sometimes friends come depends on what the equivalent of
 10  cases math homework and Chinese homework does not change in Next, a method of increasing the operating data, maintenance program for post-
 11  
12  ---------------------- -------- core idea ------------
 13                       Visitor pattern can be modified without data, a method for increasing the data manipulation operations
 14  
15  ---------------------- division of roles ---- ------------------
 16        abstract visitors (abstract Visitor class)
 17        specific visitors (Visitor access derived class teacher and parents access class)
 18        abstract elements (abstract work class)
 19        specific elements (job class derived mathematical operations and language work)
 20        object structure (member variables used to store the list element)
 21        visitor mode can modify the data without situation, a method of increasing an operation of data operation
 22  ************************************************* ********************************************** * / 
23 # the include <the iostream>
 24 #include <List>
 25  the using  namespace STD;
 26 is  
27 class Visitor
 28  {
 29  public :
 30      Virtual  void VisitMathHomework () {COUT << " 111 " << endl;};
 31 is      Virtual  void VisitChineseHomework () {};
 32  };
 33 is  
34 is  class TeacherVisitor: public Visitor
 35  {
 36  public :
 37 [      void VisitMathHomework ()
 38 is      {
 39          COUT << " teacher review mathematical operations 90 minutes " << endl;
40      }
 41 is      void VisitChineseHomework ()
 42 is      {
 43 is          COUT << " teacher review language job 100 " << endl;
 44 is      }
 45  };
 46 is  
47  class ParentVisitor: public Visitor
 48  {
 49  public :
 50      void VisitMathHomework ()
 51 is      {
 52 is          << COUT " parents to check mathematical operations " << endl;
 53 is      }
 54 is      voidVisitChineseHomework ()
 55      {
 56 is          COUT << " parents to check the language work " << endl;
 57 is      }
 58  };
 59  
60  class Homework
 61 is  {
 62 is  public :
 63 is      Virtual  void Accept (Visitor * m_abstractVisitor) {};
 64  };
 65  
66  class MathHomework: public Homework
 67  {
 68  public :
 69      void the Accept (Visitor *m_abstractVisitor)
 70     {
 71         m_abstractVisitor->VisitMathHomework();
 72     };
 73 };
 74 
 75 class ChinesehHomework:public Homework
 76 {
 77 public:
 78     void accept(Visitor *m_abstractVisitor)
 79     {
 80         m_abstractVisitor->VisitChineseHomework();
 81     };
 82 };
 83 
 84 class ObjectStructure
 85 {
 86 private:
 87     list<Homework*>elementsList;
 88 public:
 89     void addElement(Homework* m_homework)
 90     {
 91         elementsList.push_back(m_homework);
 92     }
 93     void display(Visitor *m_abstractVisitor)
 94     {
 95         for(std::list<Homework*> ::iterator iter=elementsList.begin();iter!=elementsList.end();iter++)
 96         {
 97             (*iter)->accept(m_abstractVisitor);
 98         }
 99     }
100 };
101 
102 int main()
103 {    ObjectStructure m_objectStructure;
104     m_objectStructure.addElement(new MathHomework);
105     m_objectStructure.addElement(new ChinesehHomework);
106 
107     //老师访问
108     TeacherVisitor *m_teacherVisitor=new TeacherVisitor;
109     m_objectStructure.display(m_teacherVisitor);
110 
111 
112     //家长访问
113     ParentVisitor *m_parentVisitor=newParentVisitor;
 114      m_objectStructure.display (m_parentVisitor);
 115  
1 16      getchar ();
 117      return  0 ;
 1 18  };
 119  
120  
121  
122  / * 
123  Reference Books URL:
 124  https://www.cnblogs.com/chenssy/p/ 3339756.html    to visitors about the role model description
 125  , "Westward design pattern"
 126  
127  
128  * /

 

Output:

 

Guess you like

Origin www.cnblogs.com/wuhongjian/p/11566767.html