C + + design mode to recognize visitor patterns

Source: Internet
Author: User
Visitor Mode (Visitor): Represents an operation that acts on elements in an object's structure, allowing you to define new actions that act on these elements without changing the individual element classes.

Five Role classes:

Visitor: Declares a visit operation for each class that is concreteelement in the object structure.

Concretevistor: A specific visitor that implements each action declared by visitor. Each operation implements a portion of the algorithm, and the algorithm fragment is the class that corresponds to the object in the structure.

The element defines an accept operation that takes a visitor as a parameter.

Concreteelement specific elements to implement accept operations

Objectstructure can enumerate its elements, providing a high-level interface to allow visitors to access its elements.

Applicable occasions:

It is suitable for a system with relatively stable data structure, which frees up the coupling between the structures and the operations acting on the structure, so that the set of operations can evolve relatively freely.

The purpose of the visitor pattern is to separate the processing from the data structure. Many systems can be separated according to the algorithm and data structure, if such a system has a relatively stable data structure, and there are easy to change the algorithm, the use of the visitor pattern is more appropriate, because the visitor pattern makes the operation of the algorithm easier.

The advantages:

The advantage of the visitor pattern is that it is easy to add new operations because adding new operations means adding a new visitor. The visitor pattern concentrates the behavior on a visitor object.

Its disadvantages:

Makes it difficult to add new data structures.

Pattern implementation:

[Code]class concreteelementa;class concreteelementb;class visitor{public:virtual void VisitConcreteElementA (     Concreteelementa *pelementa) = 0; virtual void Visitconcreteelementb (concreteelementb *pelementb) = 0;};     Class Concretevisitor1:public visitor{public:void Visitconcreteelementa (Concreteelementa *pElementA); void Visitconcreteelementb (Concreteelementb *pelementb);}; void Concretevisitor1::visitconcreteelementa (Concreteelementa *pelementa) {//Now according to the Pelementa passed in, The element in the Concreteelementa can be manipulated}void concretevisitor1::visitconcreteelementb (Concreteelementb *pElementB) {// Now, according to the PELEMENTB, the element in the CONCRETEELEMENTB can be manipulated}class concretevisitor2:public visitor{public:void     Visitconcreteelementa (Concreteelementa *pelementa); void Visitconcreteelementb (Concreteelementb *pelementb);}; void Concretevisitor2::visitconcreteelementa (Concreteelementa *pelementa) {//...} void Concretevisitor2::visitconcreteelementb (Concreteelementb *pelementb) {//...}//Element objectclass element{public:virtual void Accept (Visitor *pvisitor) = 0;}; Class Concreteelementa:public element{public:void Accept (Visitor *pvisitor);}; void Concreteelementa::accept (Visitor *pvisitor) {Pvisitor->visitconcreteelementa (this);} Class Concreteelementb:public element{public:void Accept (Visitor *pvisitor);}; void Concreteelementb::accept (Visitor *pvisitor) {PVISITOR-&GT;VISITCONCRETEELEMENTB (this);}     The Objectstructure class, which can enumerate its elements, can provide a high-level interface to allow visitors to access its elements class Objectstructure{public:void Attach (element *pelement);     void Detach (Element *pelement); void Accept (Visitor *pvisitor);p rivate:vector<element *> elements;}; void Objectstructure::attach (Element *pelement) {elements.push_back (pelement);} void Objectstructure::D etach (element *pelement) {vector<element *>::iterator it = Find (Elements.begin (), Element     S.end (), pelement);     if (it = Elements.end ()) {elements.erase (IT); }}void objectstructUre::accept (Visitor *pvisitor) {//Set Visitor for each Element, do the corresponding action for (vector<element *>::const_iterator it = Elements.begin (); It! = Elements.end ();     ++it) {(*it)->accept (pvisitor); }}

Test Case:

[Code]int Main () {     objectstructure *pobject = new Objectstructure;     Concreteelementa *pelementa = new Concreteelementa;     CONCRETEELEMENTB *pelementb = new Concreteelementb;     Pobject->attach (Pelementa);     Pobject->attach (PELEMENTB);     ConcreteVisitor1 *pvisitor1 = new ConcreteVisitor1;     ConcreteVisitor2 *pvisitor2 = new ConcreteVisitor2;     Pobject->accept (PVisitor1);     Pobject->accept (PVISITOR2);     if (pVisitor2) delete PVisitor2;     if (pVisitor1) delete pVisitor1;     if (PELEMENTB) delete pelementb;     if (pelementa) delete Pelementa;     if (pobject) delete pobject;     return 0;}

The above is the C + + design mode to learn the content of the visitor pattern, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.