Visitor Design Pattern
Get an overview of the visitor design pattern.
We'll cover the following...
Overview
We often have object structures in our programs. For example, we might have objects in an interface that’s implemented by various concrete classes. Sometimes, we want to add functionality to this object structure. We might be tempted to add a method for each additional functionality to the interface. However, this could violate the single responsibility and the open-closed design principles. It isn’t a good idea to modify the interface every time we need to add a functionality to the concrete objects.
Let’s assume that we have a store with different types of products, such as laptops and smartphones. Also, we have different types of customers (such as new customers, gold-level customers, and platinum-level customers). The operation that the code needs to perform is the calculation of the bill for each customer-product pair.
The visitor pattern can solve this problem ...