Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Visitor Pattern

Visitor Pattern

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

Introduction

Intent

Aims to separate the data structure from the operations performed on it, making it easier to add new operations without modifying the data structure itself.

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

Disadvantages

Usage recommendation

Notes

Main roles included

- Defines the interface for visiting elements. - Implements the visitor interface, providing access to each concrete element class and corresponding operations. - Defines a method that accepts a visitor. - Implements the element interface and provides an `accept` method that allows visitors to access and operate. - Defines how to assemble concrete elements, such as a composite class. - Use the Visitor Pattern to operate on the object structure.

Create a class that implements

We will create concrete classes of the interface that defines the ComputerPart interface.Keyboard、Mouse、Monitor and Computer is the entity class that implements ComputerPart accept operation. We will define another interface ComputerPartVisitorthat defines the operations of the visitor class.Computer Use concrete visitors to perform corresponding actions.

VisitorPatternDemo, and our demo class uses the Computer、ComputerPartVisitor classes to demonstrate the use of the Visitor Pattern.

Step 1

Define an interface representing an element.

ComputerPart.java

public

interface

ComputerPart

{

public

void

accept

(

ComputerPartVisitor

computerPartVisitor

)

;

}

Step 2

Create the entity classes that extend the above class.

Keyboard.java

public

class

Keyboard

implements

ComputerPart

{

@

Override

public

void

accept

(

ComputerPartVisitor

computerPartVisitor

)

{

computerPartVisitor

.

visit

(

this

)

;

}

}

Monitor.java

public

class

Monitor

implements

ComputerPart

{

@

Override

public

void

accept

(

ComputerPartVisitor

computerPartVisitor

)

{

computerPartVisitor

.

visit

(

this

)

;

}

}

Mouse.java

public

class

Mouse

implements

ComputerPart

{

@

Override

public

void

accept

(

ComputerPartVisitor

computerPartVisitor

)

{

computerPartVisitor

.

visit

(

this

)

;

}

}

Computer.java

public

class

Computer

implements

ComputerPart

{

ComputerPart

[

]

parts

;

public

Computer

(

)

{

parts

=

new

ComputerPart

[

]

{

new

Mouse

(

)

,

new

Keyboard

(

)

,

new

Monitor

(

)

}

;

}

@

Override

public

void

accept

(

ComputerPartVisitor

computerPartVisitor

)

{

for

(

int

i

=

0

;

i

<

parts

.

length

;

i

++

)

{

parts

[

i

]

.

accept

(

computerPartVisitor

)

;

}

computerPartVisitor

.

visit

(

this

)

;

}

}

Step 3

Define an interface representing a visitor.

ComputerPartVisitor.java

public

interface

ComputerPartVisitor

{

public

void

visit

(

Computer

computer

)

;

public

void

visit

(

Mouse

mouse

)

;

public

void

visit

(

Keyboard

keyboard

)

;

public

void

visit

(

Monitor

monitor

)

;

}

Step 4

Create concrete visitors that implement the above classes.

ComputerPartDisplayVisitor.java

public

class

ComputerPartDisplayVisitor

implements

ComputerPartVisitor

{

@

Override

public

void

visit

(

Computer

computer

)

{

System

.

out

.

println

(

"

Displaying Computer.

"

)

;

}

@

Override

public

void

visit

(

Mouse

mouse

)

{

System

.

out

.

println

(

"

Displaying Mouse.

"

)

;

}

@

Override

public

void

visit

(

Keyboard

keyboard

)

{

System

.

out

.

println

(

"

Displaying Keyboard.

"

)

;

}

@

Override

public

void

visit

(

Monitor

monitor

)

{

System

.

out

.

println

(

"

Displaying Monitor.

"

)

;

}

}

Step 5

Use ComputerPartDisplayVisitor to display the Computer components of.

VisitorPatternDemo.java

public

class

VisitorPatternDemo

{

public

static

void

main

(

String

[

]

args

)

{

ComputerPart

computer

=

new

Computer

(

)

;

computer

.

accept

(

new

ComputerPartDisplayVisitor

(

)

)

;

}

}

Step 6

Run the program, output result:



Displaying Mouse.

Displaying Keyboard.

Displaying Monitor.

Displaying Computer.