The Visitor pattern
The last pattern we are going to explore is the Visitor pattern. This pattern deals with applying customized behavior to an existing list of components that form a hierarchy (a tree or a linked list) but without changing their structure or having them implement an interface.
In practice, this means that you make your components have a method that accepts a reference of a Visitor object and passes its own instance as a parameter to this visitor. The visitor, on the other hand, will have access to each type of visited object's public methods and they can aggregate the state of each object it visits into a different result.
We'll now explain in more detail when to use this pattern.
When to use the Visitor pattern?
The primary use cases of this pattern are explained as follows. You want to use this pattern in the following cases:
- You want to abstract some functionality for collecting the public state of a hierarchy of objects: You have...