A brief introduction to visitor pattern in C++ design patterns

黄舟
Release: 2017-01-17 13:24:24
Original
1522 people have browsed it

Visitor mode (Visitor): Represents an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the class of each element.

Five role classes:

Visitor: declares a Visit operation for each class of ConcreteElement in the object structure.

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

ELement defines an Accept operation, which takes a visitor as a parameter.

ConcreteElement specific element, implements Accept operation

ObjectStructure can enumerate its elements and can provide a high-level interface to allow visitors to access its elements.

Applicable occasions:

It is suitable for systems with relatively stable data structures. It decouples the coupling between the data structure and the operations that act on the data structure, so that the operation set can be relatively free. evolution.

The purpose of the visitor pattern is to separate processing from data structures. Many systems can be separated according to algorithms and data structures. If such a system has a relatively stable data structure and an algorithm that is easy to change, it is more appropriate to use the visitor mode, because the visitor mode makes it easy to increase the number of algorithm operations. .

The advantages:

The advantage of the visitor mode is that it is easy to add new operations, because adding a new operation means adding a new visitor. The visitor pattern concentrates related behaviors into a visitor object.

The disadvantages:

Make 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)
{
     // 现在根据传进来的pElementA,可以对ConcreteElementA中的element进行操作
}

void ConcreteVisitor1::VisitConcreteElementB(ConcreteElementB *pElementB)
{
     // 现在根据传进来的pElementB,可以对ConcreteElementB中的element进行操作
}

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->VisitConcreteElementB(this);
}

// ObjectStructure类,能枚举它的元素,可以提供一个高层的接口以允许访问者访问它的元素
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::Detach(Element *pElement)
{
     vector<Element *>::iterator it = find(elements.begin(), elements.end(), pElement);
     if (it != elements.end())
     {
          elements.erase(it);
     }
}

void ObjectStructure::Accept(Visitor *pVisitor)
{
     // 为每一个element设置visitor,进行对应的操作
     for (vector<Element *>::const_iterator it = elements.begin(); it != elements.end(); ++it)
     {
          (*it)->Accept(pVisitor);
     }
}
Copy after login

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;
}
Copy after login

The above is the content of the C++ design pattern brief introduction to the visitor pattern. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template