The Visitor pattern lets you separate auxiliary operations you want to perform on existing objects into a separate class. It is especially useful when the operations don’t logically belong to the objects, the objects are fragile and hard to extend, or the objects form a composite tree.

It conforms to the Open/Closed Principle and the Single Responsibility Principle.

Using conditional logic to perform a specific operation on a matching object often runs into the problem that when target objects are hierarchically related, the compiler will not be able to match the child objects due to dynamic/lazy binding at runtime. The Visitor pattern uses double dispatch to address this problem.

Visitor pattern structure