Visitor Design Pattern In Java

Today, I am here to discuss another behavioral design pattern called Visitor Design Pattern. The Visitor design pattern lets us separate algorithms from the objects on which they operate.

Visitor Design Pattern


visitor design pattern UML diagram

To understand this, let's take an example of a Shop, which sells books, fruits, vegetables, and electronics. 

Shop Bill Processing Application Example

 Lets first define ShopItemCategory enum:

Java


Now, define the ShopItem interface:

Java


Now, define the concrete-shop-item class. Code for Book class:

Java


Code for Electronics class:

Java


Code for Fruit class:

Java


and code for Vegetable class:

Java


Since the shop get customers, let's define a Customer class:

Java


Here, I have added a method to calculate cost of the shop item as well. Also, I made the class abstract to support different concrete customer classes.

Code for Person class:

Java


And code for Student class:

Java


Now code for a utility class, BillPrinter, for printing itemized bill:

Java


Now, it's time to write the Main program to execute and test the output:

Java


and below is the output:

Java


So far so good. Now, suppose the shop-owner wants to start supporting discounts on different shop-items based on item-category, item quantity/price, and customer type (student or common person).

To minimize the changes in the shop-item classes, we will use the Visitor pattern here. Visitor pattern will handle calculating the discount for each of the shop items.

Shop Bill Processing Application Example Using Visitor Design Pattern

Let's start updating the code to implement discount calculation functionality using Visitor pattern.

Code for ShopItemCategory enum:

Java


Updated code for ShopItem interface:

Java


Since the interface will act as an Element for us, I have added the method accept() to accept the visitor object. The Visitor object will perform the operation of calculating discounts for each item. Now, we have to implement the accept() method in each concrete shop-item class.

Updated code for Book class:

Java


Updated code for Electronics class:

Java


Updated code for Fruit class:

Java


Updated code for Vegetable class:

Java


Now we will define the ShopVisitor interface to create our visit() method for each shop-item.

Java


Now, to use visitor interface, we will make our Customer class to implement it.

Java


Updated code for Person class:

Java


Updated code for Student class:

Java


So, we can see that each visitor class has its own implementation of discount calculation.

Now, we need to update BillPrinter to accommodate discount values, along with the itemized bill print.

Java


Now, it's time to write our Main program to execute and test the code:

Java


And below is the output of the program:

Java


I hope that you now have a good idea of how to use the Visitor Design Pattern.

Source Code can be found here: Visitor-Design-Pattern-Sample-Code

Liked the article? Please don't forget to press that like button. Happy coding!

Need more articles, please visit my profile: Brijesh Saxena.

 

 

 

 

Top