# What the Visitor pattern solves
# Core components in Java
- Concrete elements: classes such as Developer and Manager that implement accept() and call visitor.visit(this).
- Visitor abstraction: an interface declaring visit methods, one per concrete element type (e.g., visit(Developer d), visit(Manager m)).
- Concrete visitors: classes implementing the visitor interface to perform specific operations (report generation, export, compliance checks).
- Client: code that assembles the element collection and applies visitors to each element.
# Example structure (Employee domain)
The article uses an employee example to illustrate the pattern.
Visitor interface and concrete visitor examples follow the same structure: one visit method per concrete element type. A concrete visitor implements the logic for an operation, for example calculating bonuses or generating a report.
# How it works: double dispatch
Accept delegates control to the visitor by passing the concrete element. The visitor then invokes the appropriate visit method for that concrete type. This achieves double dispatch: runtime type of the element and the operation (visitor) determine which code runs. You avoid type checks scattered around and you can add operations by adding new visitor classes.
# When to use Visitor
- You need to add many unrelated operations over the same object structure.
- Operations need access to the concrete element types' internal data via their public interfaces.
Typical systems where this applies: compilers and interpreters (operating on AST nodes), document-processing pipelines, static-analysis tools, reporting systems.
# Trade-offs and practical considerations
- Benefits: keeps element classes small, centralizes operations in visitors, makes adding operations simple (add a new visitor).
- Costs: adding a new element type requires modifying every visitor interface and all concrete visitor classes. That can be invasive if the element set changes frequently.
# Alternatives and modern Java notes
The classic Visitor pattern requires an accept method in every element and a visitor interface with one method per element type. Newer Java features such as sealed types and exhaustive pattern matching can simplify some visitor use cases by reducing boilerplate, but they change the trade-offs: pattern matching can make adding new element types easier, while the classic Visitor still excels at adding operations without touching element classes.
# Practical checklist for adoption
- Confirm the element hierarchy is stable.
- Evaluate maintenance cost when adding new element types (you must update all visitors).
- Consider language features (sealed classes, pattern matching) that may offer alternative implementations with less ceremony.
The Visitor pattern remains a practical tool when a system's set of element types is stable and operations evolve independently. The employee example in the article shows the pattern's structure and behavior clearly, and the article maps those ideas onto real-world scenarios where the pattern simplifies code organization.