Design Mode learning-Visitor Mode

Source: Internet
Author: User

Visitor Mode

GOOD: Suitable for systems with stable data structures. It separates the data structure from the operations acting on the data structure so that the operation set

Advantage: it is easy to add a new operation, because adding a new operation is equivalent to adding a visitor. The visitor mode aggregates the related behavior into a visitor object.

[Html]
# Include <iostream>
# Include <string>
# Include <vector>
Using namespace std;
 
Class Man;
Class Woman;
// Action
Class Action
{
Public:
Virtual void GetManConclusion (Man * concreteElementA) = 0;
Virtual void GetWomanConclusion (Woman * concreteElementB) = 0;
};
// Succeeded
Class Success: public Action
{
Public:
Virtual void GetManConclusion (Man * concreteElementA)
{
Cout <"when a man succeeds, there is a great woman behind it" <endl;
}
Virtual void GetWomanConclusion (Woman * concreteElementB)
{
Cout <"when a woman succeeds, there is a useless man behind it" <endl;
}
};
 
// Failed
Class Failure: public Action
{
Public:
Virtual void GetManConclusion (Man * concreteElementA)
{
Cout <"when a man fails, there is a great woman behind it" <endl;
}
Virtual void GetWomanConclusion (Woman * concreteElementB)
{
Cout <"when a woman fails, there is a useless man behind it" <endl;
}
};
 
// Abstract human
Class Person
{
Public:
Virtual void Accept (Action * visitor) = 0;
};
 
// Man
Class Man: public Person
{
Public:
Virtual void Accept (Action * visitor)
{
Visitor-> GetManConclusion (this );
}
};
 
// Woman
Class Woman: public Person
{
Public:
Virtual void Accept (Action * visitor)
{
Visitor-> GetWomanConclusion (this );
}
};
 
// Object structure class
Class ObjectStructure
{
Private:
Vector <Person *> m_personList;
 
Public:
Void Add (Person * p)
{
M_personList.push_back (p );
}
Void Display (Action *)
{
Vector <Person *>: iterator p = m_personList.begin ();
While (p! = M_personList.end ())
{
(* P)-> Accept ();
P ++;
}
}
};
 
// Client
Int main ()
{
ObjectStructure * OS = new ObjectStructure ();
OS-> Add (new Man ());
OS-> Add (new Woman ());
 
Success * success = new Success ();
OS-> Display (success );
 
Failure * fl = new Failure ();
OS-> Display (fl );
 
Return 0;
}
 
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.