Visitor Pattern
Explore the visitor pattern to apply new operations to a collection of objects without changing their classes. This lesson helps you implement visitor logic to separate concerns and add flexibility, using a bookshop example to demonstrate applying discounts.
We'll cover the following...
What is the visitor pattern?
The visitor pattern allows the definition of new operations to the collection of objects without changing the structure of the objects themselves. This allows us to separate the class from the logic it implements.
The extra operations can be encapsulated in a visitor object. The objects can have a visit method that accepts the visitor object. The visitor can then make the required changes and perform the operations on the object that received it. This allows the developers to make future extensions, extend the libraries/frameworks, etc.