We appreciate that it’s not that easy to understand the use and point of a visitor pattern, but anyone learning to code today will certainly come into contact with this pattern.
For a simple-to-grasp analogy from a real-world situation, visitor patterns are often demonstrated using cab rides. Let’s assume that a customer calls a cab to take them to their front door. The customer gets into the ‘visiting car’, but it’s the cab (or rather, the driver) that is fully in control of transporting the person.
Shopping in a supermarket is another common example to explain how visitor patterns work. The person doing their grocery shopping puts everything they want in their shopping cart, which can be considered a set of elements from the object structure. The cashier then acts like a visitor, scanning prices and/or weighing each individual shopping item (i.e., element) to calculate the total cost.