Analysis and implementation of design patterns (c + +) 20-visitor mode

Source: Internet
Author: User

Role:

Represents 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.

UML Structure diagram:

Analytical:

The visitor mode encapsulates access to nodes as an abstract base class, generating new access patterns by deriving different classes. At the time of implementation, an interface function that accesses all the different nodes is declared in the visitor abstract base class, such as the Visitconcreateelementa function in the graph, it also creates a flaw in the visitor pattern--Adding a node to the interface function in the visitor. This allows all visitor and their derived classes to be recompiled, that is to say, a disadvantage of the visitor model is that it is difficult to add new nodes. In addition, it is also important to point out that the visitor model uses the so-called "double dispatch" technique, and takes the picture as an example, To access a node, you first need to produce an element derived class object, which is passed into a visitor class derived class object to invoke the corresponding accept function, that is to say, what kind of element is used for the visitor access, Need two times dynamic binding can be determined, the specific implementation can refer to the following implementation code in the Main.cpp part of how to invoke these classes.

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.