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
- Solves the coupling problem between stable data structures and volatile operations, allowing operations to change independently of the data structure.
Use Cases
- When multiple different and unrelated operations need to be performed on the objects in an object structure, especially when these operations need to avoid "polluting" the object classes themselves.
Implementation
- Define the visitor interface: Declares a series of visit methods, one for each element class in the data structure.
- Create concrete visitors: Implements the visitor interface and provides a concrete implementation for each visit method.
- Define the element interface: Declares a method that accepts a visitor.
- Create concrete elements: Implements the element interface; each concrete element class corresponds to a concrete object in the data structure.
Key code
- Visitor Interface: Contains methods for visiting different elements.
- Concrete visitor: Implements the visitor interface and contains the visit logic for each element class.
- Element interface: Contains a method that accepts a visitor.
- Concrete element: Implements the element interface and provides an entry point for visitors to access.
Application Example
- Visiting scenario: A visitor (such as you) visits a friend's home; the friend acts as an element providing information, and the visitor makes judgments based on the information.
Advantages
- Single responsibility principle: The Visitor Pattern complies with the Single Responsibility Principle; each class is responsible for only one responsibility.
- Extensibility: It is easy to add new operations to the data structure.
- Flexibility: Visitors can change independently of the data structure.
Disadvantages
- Violates the Law of Demeter: Elements need to expose their internal information to visitors.
- Element classes are difficult to change: Element classes need to maintain compatibility with visitors.
- Depends on concrete classes: The Visitor Pattern depends on concrete classes rather than interfaces, violating the Dependency Inversion Principle.
Usage recommendation
- Consider using the Visitor Pattern when the object structure is stable but multiple new operations need to be defined on it.
- Use the Visitor Pattern to encapsulate operations when you need to avoid operations "polluting" the object classes.
Notes
- The Visitor Pattern can be used for feature unification, such as report generation, user interface display, interceptors, and filters.
Main roles included
-
Visitor:
- Defines the interface for visiting elements.
-
Concrete Visitor:
- Implements the visitor interface, providing access to each concrete element class and corresponding operations.
-
Element:
- Defines a method that accepts a visitor.
-
Concrete Element:
- Implements the element interface and provides an
acceptmethod that allows visitors to access and operate.
-
Object Structure (optional):
- Defines how to assemble concrete elements, such as a composite class.
-
Client (optional):
- 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.