Add operations to object structures without changing their classes. You need to run multiple operations on a stable object structure. Adding new
Visitor
Overview
Add operations to object structures without changing their classes.
When to use
- You need to run multiple operations on a stable object structure.
- Adding new operations is more common than adding new node types.
Java example
TypeScript example
Pros and cons
Pros:
- New operations can be added without modifying nodes.
- Separates algorithms from object structure.
Cons:
- Adding new node types requires updating all visitors.
- Can be verbose.
Common pitfalls
- Using Visitor for small object graphs.
- Forgetting to update all visitors after new nodes.