Visitor pattern (Visitor)



 GOF Intent: Represents an operation that acts on elements in an object structure. (The element sets the accessor, and the accessor then accesses the object)

Role:

1.Visitor abstract visitor role. There is method visitElent which takes element parameter

2.ConcreteVisitor. The specific visitor role, which accesses the element object.

3. Element is the visitor, which defines an access operation (accept()), which takes a visitor (Visitor) as a parameter.

4.ConcreteElement concrete visitor.

5.ObjectStructure structure object role, which is a necessary role to use the visitor pattern. It has the following characteristics: can enumerate its elements; each sets a visitor.

 

Realistic Points:

Objects can choose a solution with different accessors

An element accepts an accessor, and the accessor accesses the element. The essence is that the element selects different accessors

The element calls accept to pass in the visitor, and the visitor calls its own method, visitorElment, which needs to pass in the element element.

 

Element itself determines what kind of visitor to use, and the visitor determines which method to use for the visitor according to what kind of Element.

There are many kinds of visitor can be VisitorA, visitorB. . . .

There are multiple methods of visitor according to Element type (Element also has multiple ElementA, ElementB...) can be visitElementA, visitElementB. . .

 

The element can choose different visitors to call the method of the visitor to access this element, and the method of the visitor to access the element to access the element

 

Double dispatch, the compile time does not know which element it is, and which kind of visitor is accpted, which is clearly determined by the runtime.

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326802297&siteId=291194637