Breaking down the Visitor pattern
As part of the Behavioral family of design patterns, the Visitor pattern is all about adding new features without changing existing structures or class hierarchies. This approach is ideal when you want to apply new requirements to pre-existing objects or when dealing with legacy code (or systems) that can’t be directly modified for one reason or another. The Visitor pattern is useful when:
- You need to add new behaviors (i.e., operations or algorithms) to existing objects without changing the object’s class.
- You want to perform operations on a set of unrelated objects that don’t share the same interfaces or parent classes.
- You want to group related sets of behaviors or algorithms that can be applied to a set of objects without adding unrelated behaviors to classes that don’t need them.
Sometimes, the party-guest-visiting analogy isn’t concrete enough to visualize, even though the pattern...