Tuesday, August 20, 2024

Visitor design pattern

 Purpose: Defines a new operation without changing the classes of the elements on which it operates. This pattern allows you to add new operations to objects without modifying them.

Vistors in the Real World 

A real world analogy always helps with the understanding of a design pattern. One example I have seen for the Visitor pattern in action is a taxi example, where the customer calls orders a taxi, which arrives at his door. Once the person sits in, the visiting taxi is in control of the transport for that person. 

Shopping in the supermarket is another common example, where the shopping cart is your set of elements. When you get to the checkout, the cashier acts as a visitor, taking the disparate set of elements (your shopping), some with prices and others that need to be weighed, in order to provide you with a total. 

 

 

 

No comments:

Post a Comment