System Design
15. Software Design PatternsAnimated walkthrough
Visitor Pattern
Add operations without changing the classes.
The idea
Elements accept a visitor, which implements the operation per element type.
Watch it work
The steps light up one at a time. Use the controls to pause or step through.
- 1Define visitor methods
- 2Elements call accept
- 3The visitor handles each type
- 4Add new visitors freely
1/4
Step by step
- 1Define visitor methods
- 2Elements call accept
- 3The visitor handles each type
- 4Add new visitors freely
Where it is used
Compilers and tree operations.
Watch out for
Adding a new element type changes every visitor.