Visitor Pattern
A behavioral design pattern that separates operations from the object structure through double dispatch, allowing new operations to be defined without modifying the object classes.
Timeline (last 90 days)
The visitor pattern creates an object for each operation; nodes implement an accept(visitor) method that internally calls the corresponding visit method.
The visitor pattern suits systems whose model is relatively stable while operations keep evolving.
The core of the visitor pattern is double dispatch: the first dispatch occurs when a node calls accept(visitor), selecting the corresponding visit_xxx method based on the node type; the second dispatch occurs inside the visit_xxx method, executing the corresponding operation based on the visitor's concrete type.
Adding a new operation requires only a new visitor class without modifying the node classes, in accordance with the open-closed principle.
Adding a new node type requires modifying all visitor interfaces and their implementations, so the visitor pattern is best suited to scenarios where node types are stable and operations change frequently.
Each checking rule in Checkstyle is essentially an AST visitor, employing the Visitor pattern (one of the GoF 23 design patterns).
All Facts (6)
The visitor pattern creates an object for each operation; nodes implement an accept(visitor) method that internally calls the corresponding visit method.
50%UnverifiedThe visitor pattern suits systems whose model is relatively stable while operations keep evolving.
50%UnverifiedThe core of the visitor pattern is double dispatch: the first dispatch occurs when a node calls accept(visitor), selecting the corresponding visit_xxx method based on the node type; the second dispatch occurs inside the visit_xxx method, executing the corresponding operation based on the visitor's concrete type.
50%UnverifiedAdding a new operation requires only a new visitor class without modifying the node classes, in accordance with the open-closed principle.
50%UnverifiedAdding a new node type requires modifying all visitor interfaces and their implementations, so the visitor pattern is best suited to scenarios where node types are stable and operations change frequently.
50%UnverifiedEach checking rule in Checkstyle is essentially an AST visitor, employing the Visitor pattern (one of the GoF 23 design patterns).
50%