Visitor design pattern of C # design pattern (Visitor design pattern)

Source: Internet
Author: User
Tags data structures

One, visitor (Visitor) mode

The purpose of the visitor pattern is to encapsulate actions that are applied to some data structure element. Once these operations need to be modified, the data structure that accepts the operation can remain unchanged.

Questions raised

A large number of collection manipulation objects are provided under the System.Collection namespace. But most of the cases are handled by the aggregation of similar objects. In other words, the actions taken on the aggregation are similar operations for objects of the same type. But what if you take some action on a cluster that holds different types of objects?

Coarse looks, this does not seem to be any difficulty. However, if you need to take some action against a cluster that contains different types of elements, and the details of the operation vary depending on the type of element, a conditional transfer statement must be made to type the element type. At this point, the use of the visitor pattern is a solution worth considering.

Visitor mode

The visitor pattern is suitable for systems with relatively undetermined data structures, which frees up the coupling between the structure and the operations acting on the structure, so that the set of operations can evolve relatively freely.

Each node of the data structure can accept a call from a visitor, which passes in the node object to the Visitor object, which in turn performs the operation of the Node object. Such a process is called "Double dispatch". The node invokes the visitor, passing it in itself, and the visitor executes an algorithm against the node.

Double dispatch means that the actions exerted on the node are based on the data type of the visitor and the node itself, not just the one.

II. structure of the visitor pattern

As shown in the following illustration, this static diagram shows the design of a visitor pattern with two specific visitors and two specific nodes, and it must be noted that there is no correlation between the number of specific visitors and the number of specific nodes, although the number of both is two in this schematic system.

The visitor pattern involves the abstract visitor role, the specific visitor role, the abstract node role, the specific node role, the Structure object role, and the client role.

Abstract visitor (Visitor) role: declares one or more access operations to form the interfaces that all the specific element roles must implement.

A specific visitor (Concretevisitor) role: Implements the interface declared by the abstract visitor role, which is the individual access actions declared by the abstract visitor.

Abstract node role: declares an accept operation that accepts a visitor object as a parameter.

Specific node role: implements the accepted operation as defined by the abstract element.

struct object (obiectstructure) role: There are some responsibilities to traverse all elements of the structure and, if necessary, provide a high-level interface that allows visitors to access each element, and, if necessary, design a composite object or a clustered such as a column (list) or a collection (set).

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.