Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
-Design Patterns, GoF
One way to implement an operation that involves the objects in a complex structure is to provide the logic in each of their classes to support the operation. The Visitor pattern provides an alternate way to implement such operations that avoids complicating the classes of the objects in the structure by putting all the necessary logic in a separate class. The Visitor pattern also allows the logic to be varied by using different Visitor classes.
-Patterns in Java, Mark Grand
1. When would you use the Visitor pattern?
2. How stable should the Element hierarchy be before you consider the Visitor pattern? What happens when a new Element is added?
3. How does the Visitor centralize related behavior? Why would this be a good thing?
4. How can the Visitor be used with the Iterator? What problems might there be?
5. In what situations would it be helpful for the Visitor to accumulate state?
6. Does the Visitor force you to break encapsulation in the Element class?
7. What is a double-dispatch?
8. Who is responsible for object traversal?
Examples – Which are Strategies?
· Java Tip 98: Reflect on the Visitor Design Pattern, Jeremy Blosser (JavaWorld.com)
· Design Patterns: Visitor (Data & Object Factory)