- The Visitor pattern defines and performs new operations on all the elements of an existing structure, without altering its classes.
- The Visitor pattern is intended for object structures, so it is often used in conjunction with the Composite pattern. The Composite pattern is responsible for setting up the multilevel structure on which the Visitor pattern then operates.
Class diagram: (see Visitor Pattern code review)
Use the Visitor pattern when:
- You have a class hierarchy that is effectively sealed.
- There are many distinct operations to perform on it.
- You need the flexibility to define new operations over time.