6.27. Visitor mode

发布时间 : 2025-10-25 13:34:19 UTC      

Page Views: 10 views

In the visitor pattern (Visitor Pattern), we use a visitor class that changes the execution algorithm of the element class. In this way, the execution algorithm of the element can change as the visitor changes. This type of design pattern belongs to behavioral pattern. According to the pattern, the element object has accepted the visitor object so that the visitor object can handle operations on the element object.

6.27.1. Introduction

意图: The data structure is mainly separated from the data operation.

主要解决: The coupling problem of stable data structure and variable operation.

何时使用: You need to do many different and unrelated operations on objects in an object structure, and you need to avoid letting these operations “pollute” the classes of these objects, and use the visitor pattern to encapsulate them in the class.

如何解决: Add an interface to the visited class that provides reception to the visitor.

关键代码: There is a method in the data base class that accepts the visitor and passes its own reference to the visitor.

应用实例: You are a guest at a friend’s house, you are a visitor, a friend accepts your interview, and you make a judgment on the friend’s description through the friend’s description, which is the visitor pattern.

优点: 1. Comply with the principle of single responsibility. 2. Excellent expansibility. 3. Flexibility.

缺点: 1. The disclosure of details by specific elements to visitors violates the Dimitt principle. 2. It is difficult to change specific elements. 3. It violates the principle of dependency inversion, relies on concrete classes, and does not rely on abstraction.

使用场景: 1. The class corresponding to the object in the object structure rarely changes, but it is often necessary to define new operations on this object structure. 2. You need to perform many different and irrelevant operations on the objects in an object structure, and you need to avoid letting these operations “pollute” the classes of these objects, and you don’t want to modify these classes when adding new operations.

注意事项: Visitors can unify the functions and can do reports, UI, interceptors and filters.

6.27.2. Realize

We will create a definition that accepts the operation ComputerPart Interface. Keyboard Mouse Monitor And Computer It came true. ComputerPart The entity class of the interface. We will define another interface ComputerPartVisitor Which defines the operation of the visitor class Computer Use entity visitors to perform the appropriate actions.

VisitorPatternDemo Our demo class uses the Computer ComputerPartVisitor Class to demonstrate the use of the visitor pattern.

访问者模式的 UML 图

6.27.3. Step 1

Define an interface that represents an element.

ComputerPart.java

publicinterfaceComputerPart{publicvoidaccept(ComputerPartVisitorcomputerPartVisitor);} 

6.27.4. Step 2

Create an entity class that extends the above class.

Keyboard.java

publicclassKeyboardimplementsComputerPart{@Overridepublicvoidaccept(ComputerPartVisitorcomputerPartVisitor){computerPartVisitor.visit(this);}} 

Monitor.java

publicclassMonitorimplementsComputerPart{@Overridepublicvoidaccept(ComputerPartVisitorcomputerPartVisitor){computerPartVisitor.visit(this);}} 

Mouse.java

publicclassMouseimplementsComputerPart{@Overridepublicvoidaccept(ComputerPartVisitorcomputerPartVisitor){computerPartVisitor.visit(this);}} 

Computer.java

publicclassComputerimplementsComputerPart{ComputerPart[]parts;publicComputer(){parts=newComputerPart[]{newMouse(),newKeyboard(),newMonitor()};}@Overridepublicvoidaccept(ComputerPartVisitorcomputerPartVisitor){for(inti=0;i<parts.length;i++){parts[i].accept(computerPartVisitor);}computerPartVisitor.visit(this);}} 

6.27.5. Step 3

Define an interface that represents visitors.

ComputerPartVisitor.java

publicinterfaceComputerPartVisitor{publicvoidvisit(Computercomputer);publicvoidvisit(Mousemouse);publicvoidvisit(Keyboardkeyboard);publicvoidvisit(Monitormonitor);} 

6.27.6. Step 4

Create entity visitors that implement the above classes.

ComputerPartDisplayVisitor.java

publicclassComputerPartDisplayVisitorimplementsComputerPartVisitor{@Overridepublicvoidvisit(Computercomputer){System.out.println("Displaying Computer.");}@Overridepublicvoidvisit(Mousemouse){System.out.println("Displaying Mouse.");}@Overridepublicvoidvisit(Keyboardkeyboard){System.out.println("Displaying Keyboard.");}@Overridepublicvoidvisit(Monitormonitor){System.out.println("Displaying Monitor.");}} 

6.27.7. Step 5

Use ComputerPartDisplayVisitor To show Computer An integral part of.

VisitorPatternDemo.java

publicclassVisitorPatternDemo{publicstaticvoidmain(String[]args){ComputerPartcomputer=newComputer();computer.accept(newComputerPartDisplayVisitor());}} 

6.27.8. Step 6

Execute the program and output the result:

Displaying Mouse. Displaying Keyboard. Displaying Monitor. Displaying Computer. 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。