Visitor Pattern
Contents
Adds new operations and behaviours to existing objects without modifying them.
Able to separate an algorithm from the object on which it operates.
- "Double Dispatch" technique
- An object takes in a visitor, and calls the right visitor method with the needed data
ie Car
| |
- Define a
Visitorclass which knows of the classes it will visit- Has a
visit(T)method for each class type - In the implementation, do whatever you need
- Has a
- Define an interface
Visitablewhich exposes anaccept(Visitor)method- In the implementation,
<Visitor>.visit(this)
- In the implementation,
A new class would require a new visit method to be added to each visitor...
