The visitor design pattern provides a method of separating an algorithm on an object and the object's actual class implementation. This allows the programmer to easily follow the open/closed principle;
software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification
Object-Oriented Software Construction, Bertrand Meyer
That is, modifying an entity's behaviour without modifying the underlying source code. Following the open/closed principle provides many quality-related benefits as the original code never changes.
Benefits
- Follows the open/closed principle
- Allows a new operation to be defined without changing the implementation of the class
- A visitor object can have state
Drawbacks
- If a new visitable object is added then all visitors need to be modified