Visitor Design Pattern In Java
Join the DZone community and get the full member experience.Join For Free
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
- The Visitor Design Pattern provides a way of separating an algorithm from an object structure on which it operates.
- A practical result of this separation is the ability to add new operations to existing object structures without modifying the structures. It is one way to follow the open/closed principle.
- The Visitor Design Pattern is used when we like to perform an operation on a group/family of objects.
- The Visitor Design Pattern is one of the twenty-three well-known GoF design patterns which helps us to move the operational logic from the objects to another class.
- The Visitor allows adding new virtual functions to a family of classes, without modifying the classes.
- Instead, a visitor class is created that implements all of the appropriate specializations of the virtual function.
- The Visitor Design Pattern makes it possible to define a new operation for (some) classes of an object structure without changing the classes.
- When new operations are needed frequently and the object structure consists of many unrelated classes, it's inflexible to add new sub-classes each time a new operation is required.
- The Element accepts the visitor and delegates the operation to the accepted Visitor object.
- The Visitor object performs the operation on the element ("visits the element").
- The Visitor Design Pattern makes it possible to create new operations independently from the classes of an object structure by adding new visitor objects.
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:
Now, define the ShopItem interface:
Now, define the concrete-shop-item class. Code for Book class:
Code for Electronics class:
Code for Fruit class:
and code for Vegetable class:
Since the shop get customers, let's define a Customer class:
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:
And code for Student class:
Now code for a utility class, BillPrinter, for printing itemized bill:
Now, it's time to write the Main program to execute and test the output:
and below is the output:
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:
Updated code for ShopItem interface:
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:
Updated code for Electronics class:
Updated code for Fruit class:
Updated code for Vegetable class:
Now we will define the ShopVisitor interface to create our
visit() method for each shop-item.
Now, to use visitor interface, we will make our Customer class to implement it.
Updated code for Person class:
Updated code for Student class:
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.
Now, it's time to write our Main program to execute and test the code:
And below is the output of the program:
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.
Opinions expressed by DZone contributors are their own.