The Visitor Design Pattern is a behavioral pattern that lets you separate algorithms from the objects on which they operate. It involves creating a visitor object that implements a set of operations to be performed on elements of an object structure. This allows you to add new operations to existing object structures without modifying their classes, adhering to the open/closed principle.
- Museum Guide: A tour guide (visitor) provides different explanations based on the type of exhibit (painting, sculpture, artifact), without altering the exhibits themselves.
- Tax Auditor: An auditor visits different types of businesses (restaurants, factories, shops) and applies different auditing rules based on business type.
- Animal Checkup: A vet (visitor) visits various animals in a zoo. Each animal type requires a specific health check, and the animal accepts the vet's visit.
- Quality Inspector in a Factory: The inspector checks different machines and applies type-specific rules, like testing, cleaning, or calibration.
- Compilers or Interpreters: In Abstract Syntax Trees (ASTs), a visitor can traverse and operate on different node types (e.g., expression, statement, declaration) without modifying the node classes.
- Object Serialization/Exporting: When exporting objects to XML, JSON, or other formats, visitors can traverse object structures and handle serialization in a clean, extensible way.
- Operations on Complex Structures: In hierarchical data models like file systems or document object models (DOM), visitors allow adding new operations (like compression, scanning, indexing) without altering element classes.
- Double Dispatch: The pattern relies on double dispatch, where the object structure calls accept(visitor), and the visitor then calls a method specific to the object's class.
- Extend Behavior Without Modifying Classes: Visitor enables adding operations to a group of classes without changing their source code.
- Tight Coupling to Object Structure: Adding a new element type to the structure requires updating all visitors — this makes the pattern great for stable structures with changing behaviors.
- Centralizes Operations: Logic for multiple operations is centralized in visitor classes, separating them from the data classes and enhancing maintainability.