In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how C++ implements the visitor mode. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
I. Visitor pattern basics 1.1 pattern motivation
There may be many different types of elements in some objects in the system, and different callers, called visitors, use these elements differently.
Visitor pattern (Visitor Pattern): represents an operation that acts on elements in an object structure, allowing us to define new operations that act on those elements without changing their classes.
The application scenario of the visitor pattern:
The visitor pattern is suitable for systems with relatively stable data structures.
It detaches the coupling between the data structure and the operations acting on the data structure, so that the set of operations can evolve relatively freely.
The purpose of the Visitor pattern is to separate processing from data structures. If such a system has a relatively stable data structure and has changed algorithms, it is more appropriate to use the visitor pattern, because the visitor pattern makes it easier to increase the operation of the algorithm. And vice versa.
1.2 Visitor pattern structure
The object structure in the visitor pattern stores different types of element objects to provide access to different visitors. The visitor pattern consists of two hierarchies, one is the visitor hierarchy, which provides abstract and concrete visitors, and the other is the element hierarchy, which provides abstract and concrete elements.
The same visitors can access different elements in different ways, and the same elements can be accessed by different visitors in different ways.
(1) Visitor (abstract visitor)
Abstract visitors declare an access operation for each concrete element class in the object structure class.
(2) ConcreteVisitor (specific visitors)
Concrete visitors implement each operation declared by an abstract visitor, each of which is used to access an element of an element type in the object structure.
(3) Element (abstract elements)
Abstract elements are typically abstract classes or interfaces that define an accept () method that takes an abstract visitor as a parameter.
(4) ConcreteElement (specific elements)
The specific visitor implements the accept () method in its accept ()
(5) ObjectStructure (object structure)
An object structure is a collection of elements that holds element objects and provides a way to traverse its internal elements.
1.3 advantages and disadvantages of the visitor model
The advantages of Visitor Mode:
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 centralizes the relevant behavior into a visitor object.
Disadvantages of the pattern:
Makes it difficult to add new element classes. In the visitor pattern, each addition of a new element means adding a new abstract operation to the abstract visitor role and adding a corresponding concrete operation to each specific visitor, which violates the requirements of the "open-close principle".
1.4 the Visitor pattern applies # include # include using namespace std; class ConcreteElementA;class ConcreteElementB; / * Abstract visitors declare methods to access element objects, usually providing an access method for each type of element object * / class Visitor {public: virtual void VisitConcreteElementA (ConcreteElementA * pElementA) = 0; virtual void VisitConcreteElementB (ConcreteElementB * pElementB) = 0;} / * specific visitors are used to define operations on different types of element objects * / class ConcreteVisitor1: public Visitor {public: void VisitConcreteElementA (ConcreteElementA * pElementA) {/ / now according to the incoming pElementA, you can operate on the element in the ConcreteElementA} void VisitConcreteElementB (ConcreteElementB * pElementB) {/ / now you can operate on the element in the ConcreteElementB based on the incoming pElementB} / * concrete visitor 2*/class ConcreteVisitor2: public Visitor {public: void VisitConcreteElementA (ConcreteElementA * pElementA) {} void VisitConcreteElementB (ConcreteElementB * pElementB) {}}; / * Abstract element class declares the accept () method, which is used to accept the visitor's visit * / class Element {public: virtual void Accept (Visitor * pVisitor) = 0rampact accept for the visitor's visit} / * the concrete element class accesses the element by calling the visit () method of the Visitor class * / class ConcreteElementA: public Element {public: void Accept (Visitor * pVisitor) / / access to the element object by calling the visit () method of the visitor object {pVisitor- > VisitConcreteElementA (this);}; / * concrete element class * / class ConcreteElementB: public Element {public: void Accept (Visitor * pVisitor) {pVisitor- > VisitConcreteElementB (this) }}; / / ObjectStructure class (object structure class), which enumerates its elements and provides a high-level interface to allow visitors to access its elements class ObjectStructure {public: void Attach (Element * pElement) {elements.push_back (pElement);} void Detach (Element * pElement) {vector::iterator it = find (elements.begin (), elements.end (), pElement) If (it! = elements.end ()) {elements.erase (it);}} void Accept (Visitor * pVisitor) {/ / set visitor for each element and perform the corresponding operation for (vector::const_iterator it = elements.begin (); it! = elements.end (); + + it) {(* it)-> Accept (pVisitor) }} int main () {/ / instantiate the object structure for storing element objects, providing a method to traverse its internal elements ObjectStructure * pObject = new ObjectStructure; / / instantiate specific elements and put the created elements into the object structure ConcreteElementA * pElementA = new ConcreteElementA; ConcreteElementB * pElementB = new ConcreteElementB; pObject- > Attach (pElementA); pObject- > Attach (pElementB) / / instantiate the visitor ConcreteVisitor1 * pVisitor1 = new ConcreteVisitor1; ConcreteVisitor2 * pVisitor2 = new ConcreteVisitor2; / / call the accept method to accept access to the visitor object 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 } this is the end of the article on "how C++ implements the Visitor Mode". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.