Design Pattern _ visitor _ Visitor pattern

Source: Internet
Author: User

Image example:

When Valentine's day arrives, each mm needs to be given a bunch of flowers and a card, but each mm needs a needle for her personal characteristics. Each card also needs to be picked according to her personal characteristics, I should ask the florist and the gift shop owner for a visitor. Let the florist select a bunch of flowers based on the characteristics of mm and let the gift shop owner select a card based on the characteristics of each person, this makes it much easier;

Visitor mode:

The visitor mode aims to encapsulate operations that are applied to certain data structure elements. Once these operations need to be modified, the data structure that accepts the operation can remain unchanged. The visitor mode is suitable for systems with relatively undetermined data structures. It frees the coupling between the data structure and the operations acting on the structure, allowing the operation set to evolve freely. The visitor mode makes it easy to add new operations, that is, to add a new visitor class. In the visitor mode, the related behaviors are concentrated in a visitor object, rather than scattered into node classes. When using the visitor mode, put as many object browsing logic as possible in the visitor class, rather than in its subclass. The visitor mode allows you to access member classes of different hierarchies over the hierarchical structure of several classes.

Purpose:
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 classes of each element.

UML structure diagram:


Resolution:
The visitor mode encapsulates access to a node into an abstract base class, and generates a new access method by deriving different classes. during implementation, the visitor abstract base class declares the interface functions for access to all different nodes, and the visitconcreateelementa function in, this also causes a defect in the visitor mode-when a new node is added, the access interface function of the visitor must be added, in this way, all the visitor and Its Derived classes must be re-compiled. That is to say, the disadvantage of the visitor mode is that it is very difficult to add new nodes. in addition, it should be pointed out that the visitor mode adopts the so-called "Double dispatch" technology. As an example, you need to access a certain node, first, you need to generate an element derived class object, and then pass in a visitor class derived class object to call the corresponding accept function. That is to say, Which element is used for access, it can be determined only after two dynamic bindings. For specific implementation, refer to the main. the CPP part is about how to call these classes.

Design Pattern _ visitor _ Visitor pattern

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.