C + + Design pattern Visitor Pattern _c language

Source: Internet
Author: User

Objective

This is the last of the 23+1 (Simple factory model)-the visitor pattern. The visitor pattern is also a cumbersome design pattern. I also have no actual combat experience, and the understanding of visitor patterns comes entirely from the GOF design pattern: The basis of reusable object-oriented software, and this article is written based on the understanding of the book. Reading the design pattern: The basics of reusable object-oriented software, reminds me of a project I've done that doesn't use the visitor pattern, but today it's understood, and it would be a good idea to use that pattern to refactor previously done projects.

Visitor mode

In Gof's design pattern: The basics of reusable object-oriented software, the visitor pattern is said to represent an operation that acts on the elements of an object structure. It allows you to define new operations that act on these elements without changing the classes of each element. The visitor pattern frees up the coupling between the data structure and the operations acting on the structure, allowing the set of operations to evolve relatively freely. The purpose of this pattern is to separate the processing from the data structure. The visitor pattern makes it easy to add new operations because adding new operations means adding a new visitor. The visitor pattern centralizes the behavior in a visitor object. Now let's talk about the project I've been through before.

A project based on Windows shell development that stores a lot of shell items in a container while defining operations on items, and as the project has been expanding late, the operations of items need to be extended later, and now the practice is, Defines an action class that defines a set that holds items and expands the corresponding action method in the action class. Now think about it if you use the visitor pattern, because the items collection is fixed, and when you need to extend the operation of the collection, you just need to add the corresponding visitors.

UML Class Diagram

Visitor (visitor): declares a visit action for each class of concreteelement in the object structure. The name and characteristics of the operation identify the class that sent the visit request to the visitor. This allows the visitor to determine the specific class of the element being accessed. This allows visitors to access it directly through the element's specific interface.
Concretevisitor (Specific visitor): Implements each action declared by the visitor. Each operation implements part of the algorithm, which is the class that corresponds to the object in the structure. Concretevisitor provides the context for the algorithm and stores its local state. This state often accumulates results in the process of traversing the structure.
Element (elements): Defines a accept action, which takes a visitor as an argument.
Concreteelement (concrete Element): Implements the accept operation, which takes one visitor as the parameter.
Objectstructure (object structure): The ability to enumerate its elements while providing a high-level interface to allow the visitor to access its elements.

Use occasion

1. An object structure consists of many classes of objects, which have different interfaces, and you want to implement some operations that depend on their specific classes for these objects;
2. You need to do a lot of different and unrelated operations on objects in an object structure, and you want to avoid classes that let these operations "contaminate" these objects. Visitor allows you to centralize related operations and define them in a class;
3. When the object structure is shared by many applications, the visitor mode allows each application to contain only the operations that need to be used;
4. Classes that define object structures rarely change, but often require new operations to be defined on this structure. Changing the object structure class requires redefining the interface to all visitors, which can be costly. If the object structure classes change frequently, it may be better to define them in these classes.

Code implementation

Copy Code code as follows:

#include <iostream>
#include <vector>
using namespace Std;

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, based on the incoming Pelementa, you can manipulate the element in Concreteelementa
}

void Concretevisitor1::visitconcreteelementb (Concreteelementb *pelementb)
{
Now, based on the incoming PELEMENTB, you can manipulate the element in CONCRETEELEMENTB
}

Class Concretevisitor2:public Visitor
{
Public
void Visitconcreteelementa (Concreteelementa *pelementa);
void Visitconcreteelementb (Concreteelementb *pelementb);
};

void Concretevisitor2::visitconcreteelementa (Concreteelementa *pelementa)
{
// ...
}

void Concretevisitor2::visitconcreteelementb (Concreteelementb *pelementb)
{
// ...
}

Element Object
Class 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 enumerates 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);

Private
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 (), Elements.end (), pelement);
if (it!= elements.end ())
{
Elements.erase (IT);
}
}

void Objectstructure::accept (Visitor *pvisitor)
{
Set visitor for each element, and perform the corresponding action
For (vector<element *>::const_iterator it = Elements.begin (); it!= elements.end (); ++it)
{
(*it)->accept (pvisitor);
}
}

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;
}

Summarize

The basic idea of the visitor pattern is as follows: First, there is an object structure consisting of many objects, the objectstructure in the code above, whose classes have a accept method to accept the visitor object; The visitor is an interface, it has a visit method, This method does different things to different types of elements in the object structure accessed; In the process of an object structure, we traverse the entire object structure, implement the Accept method for each element, and recall the visit method of the visitor in the accept method of each element. This allows visitors to handle each element of the object structure. We can design different visitor classes for the object structure to do different things.

One of the most common words in design patterns is: Discovering changes and encapsulating them. Whether to use the visitor pattern depends on what "change" is. In the visitor pattern, "change" is the specific visitor, followed by the object structure, but if the specific elements also change, you can never use the visitor mode, because this "far-reaching", later maintenance is too bad.

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.