Summary
The Visitor design pattern is one of my all-time favorites because it provides an elegant, practical solution to a pervasive problem – how do we update existing classes without changing the class itself or needlessly creating messy class hierarchies? Not only that, but the way concrete visitors seem to naturally help you think about what behaviors should be grouped together and what concrete elements need to be involved is a great boost when thinking through new features.
Remember, the Visitor pattern is ideal for applying new behavior to objects without changing the objects themselves. Concrete elements you want to visit don’t have to be related or in the same hierarchy and concrete visitors naturally group sets of related behaviors (or operations) together instead of adding them into unrelated classes. Composite elements (elements with sub-elements) are responsible for applying a visitor to their children, which means you can choose to visit every element...