Visitor Design Pattern in Java

Filed Under: Design Patterns

Visitor Design Pattern is one of the behavioral design pattern.

Visitor Design Pattern

Visitor pattern is used when we have to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects to another class.

For example, think of a Shopping cart where we can add different type of items (Elements). When we click on checkout button, it calculates the total amount to be paid. Now we can have the calculation logic in item classes or we can move out this logic to another class using visitor pattern. Let’s implement this in our example of visitor pattern.

Visitor Design Pattern Java Example

To implement visitor pattern, first of all we will create different type of items (Elements) to be used in shopping cart.

ItemElement.java


package com.journaldev.design.visitor;

public interface ItemElement {

	public int accept(ShoppingCartVisitor visitor);
}

Notice that accept method takes Visitor argument. We can have some other methods also specific for items but for simplicity I am not going into that much detail and focusing on visitor pattern only.

Let’s create some concrete classes for different types of items.

Book.java


package com.journaldev.design.visitor;

public class Book implements ItemElement {

	private int price;
	private String isbnNumber;
	
	public Book(int cost, String isbn){
		this.price=cost;
		this.isbnNumber=isbn;
	}
	
	public int getPrice() {
		return price;
	}

	public String getIsbnNumber() {
		return isbnNumber;
	}

	@Override
	public int accept(ShoppingCartVisitor visitor) {
		return visitor.visit(this);
	}

}

Fruit.java


package com.journaldev.design.visitor;

public class Fruit implements ItemElement {
	
	private int pricePerKg;
	private int weight;
	private String name;
	
	public Fruit(int priceKg, int wt, String nm){
		this.pricePerKg=priceKg;
		this.weight=wt;
		this.name = nm;
	}
	
	public int getPricePerKg() {
		return pricePerKg;
	}


	public int getWeight() {
		return weight;
	}

	public String getName(){
		return this.name;
	}
	
	@Override
	public int accept(ShoppingCartVisitor visitor) {
		return visitor.visit(this);
	}

}

Notice the implementation of accept() method in concrete classes, its calling visit() method of Visitor and passing itself as argument.

We have visit() method for different type of items in Visitor interface that will be implemented by concrete visitor class.

ShoppingCartVisitor.java


package com.journaldev.design.visitor;

public interface ShoppingCartVisitor {

	int visit(Book book);
	int visit(Fruit fruit);
}

Now we will implement visitor interface and every item will have it’s own logic to calculate the cost.

ShoppingCartVisitorImpl.java


package com.journaldev.design.visitor;

public class ShoppingCartVisitorImpl implements ShoppingCartVisitor {

	@Override
	public int visit(Book book) {
		int cost=0;
		//apply 5$ discount if book price is greater than 50
		if(book.getPrice() > 50){
			cost = book.getPrice()-5;
		}else cost = book.getPrice();
		System.out.println("Book ISBN::"+book.getIsbnNumber() + " cost ="+cost);
		return cost;
	}

	@Override
	public int visit(Fruit fruit) {
		int cost = fruit.getPricePerKg()*fruit.getWeight();
		System.out.println(fruit.getName() + " cost = "+cost);
		return cost;
	}

}

Lets see how we can use visitor pattern example in client applications.

ShoppingCartClient.java


package com.journaldev.design.visitor;

public class ShoppingCartClient {

	public static void main(String[] args) {
		ItemElement[] items = new ItemElement[]{new Book(20, "1234"),new Book(100, "5678"),
				new Fruit(10, 2, "Banana"), new Fruit(5, 5, "Apple")};
		
		int total = calculatePrice(items);
		System.out.println("Total Cost = "+total);
	}

	private static int calculatePrice(ItemElement[] items) {
		ShoppingCartVisitor visitor = new ShoppingCartVisitorImpl();
		int sum=0;
		for(ItemElement item : items){
			sum = sum + item.accept(visitor);
		}
		return sum;
	}

}

When we run above visitor pattern client program, we get following output.


Book ISBN::1234 cost =20
Book ISBN::5678 cost =95
Banana cost = 20
Apple cost = 25
Total Cost = 160

Notice that implementation if accept() method in all the items are same but it can be different, for example there can be logic to check if item is free then don’t call the visit() method at all.

Visitor Design Pattern Class Diagram

Class diagram for our visitor design pattern implementation is:

visitor design pattern, visitor pattern class diagram

Visitor Pattern Benefits

The benefit of this pattern is that if the logic of operation changes, then we need to make change only in the visitor implementation rather than doing it in all the item classes.

Another benefit is that adding a new item to the system is easy, it will require change only in visitor interface and implementation and existing item classes will not be affected.

Visitor Pattern Limitations

The drawback of visitor pattern is that we should know the return type of visit() methods at the time of designing otherwise we will have to change the interface and all of its implementations. Another drawback is that if there are too many implementations of visitor interface, it makes it hard to extend.

Thats all for visitor design pattern, let me know if I have missed anything. Please share it with others also if you liked it.

Comments

  1. Leandro Maro says:

    Using java 8 the calculate method could be rewritten into this:

    private static int calculatePrice(ItemElement[] items) {
    ShoppingCartVisitor visitor = new ShoppingCartVisitorImplementation();
    return of(items).mapToInt(itemElement -> itemElement.accept(visitor)).sum();
    }

  2. shashi says:

    One can write a generic visit() method …that way you don;t have to change the interface each time a new ItemElement is created.

    @Override
    public int visit(ItemElement item) {

    if (item instanceof Book) {
    Book book = (Book) item;
    int cost = 0;
    // apply 5$ discount if book price is greater than 50
    if (book.getPrice() > 50) {
    cost = book.getPrice() – 5;
    } else
    cost = book.getPrice();
    System.out.println(“Book ISBN::” + book.getIsbnNumber()
    + ” cost = ” + cost);
    return cost;
    } else if (item instanceof Fruit) {
    Fruit fruit = (Fruit) item;
    int cost = fruit.getPricePerKg() * fruit.getWeight();
    System.out.println(fruit.getName() + ” cost = ” + cost);
    return cost;
    }
    // default return nothing
    return 0;
    }

  3. robothy says:

    It’s a nice example of Visitor Design Pattern, I hava some personal opinions about “Visitor Pattern Benefits”. In visitor design pattern, adding a new operation is easy, not adding an item. If we want add a operation such as get summary weight( if possible ), we just need to add an ShoppingCartVisitor’s implementation. However, adding an item will add a file and modify two files, I think it’s not easy . 🙂

  4. yami says:

    i’m not sure how :

    “`
    return visitor.visit(this);
    “`

    gives us ShoppingCartVisitorImpl method.

    Does it automaticly find implementation of interface and uses it ?

    1. just me says:

      In runtime the jvm knows it’s implementation thanks to dynamic binding.

  5. Parmod says:

    Thanks.. you explained with great details

  6. vqa nguyen says:

    For me, just do simply like this

    public interface ItemElement {

    public int prix ();
    }

    public static class Fruit implements ItemElement {

    ….

    @Override
    public int prix() {
    int cost = getPricePerKg()*getWeight();
    System.out.println(getName() + ” cost = “+cost);
    return cost;
    }

    }

    public static class Book implements ItemElement {

    …..
    @Override
    public int prix() {
    int cost=0;
    //apply 5$ discount if book price is greater than 50
    if(getPrice() > 50){
    cost = getPrice()-5;
    }else cost = getPrice();
    System.out.println(“Book ISBN::”+getIsbnNumber() + ” cost =”+cost);
    return cost;
    }

    }

    The question is what is the advantage of using VisitorPattern ???

    1. Mark Valley says:

      This example is a little bit simple to show the pattern applicability.

      Image you have to add the freight to the price calculation. Freight price will vary according to the region. These rules should not be calculated inside the Product classes as they don’t cara about freight costs.

      One possible solution would be the creation of multiple concrete visitors, one for each region. These visitors will hold the responsability to calculate the freight costs and return the total price.

      public SouthRegionVisitor implements ShoppingCartVisitor {

      public int visit (Book book) {
      int freightCost = calculateFreightForSouthRegion(book.getWeight());
      return book.price + freightCost;
      }
      }

      public NorthRegionVisitor implements ShoppingCartVisitor { // and so on

      1. Techguynarendra says:

        Nice point

  7. MaikoID says:

    Hi, I’ve a doubt why the accept methods from Book and Fruit need to have a ShoppingCartVisitor instead of a Visitor one? A ShoppingCartVisitor needs to implements the Visitor interface hence we can allow the polymorphism do its job. If we insist to only use the ShoppingCartVisitor we lose the hability to accept Visitor for more than one Class. Am I right?

    BR

    1. HieuDT says:

      i total agree with you.

  8. Jay says:

    I would like to visit() each of the below comments (in a loop) and accept() what they have said and collate it for you!!!!

    Thanks.

  9. Vijay says:

    The best explanation of Visitor Pattern I found in net. Great Work

  10. Maurice Kingsley says:

    Thank you for this simple but all explaining example of a fairly neglected Java pattern. Good job!

  11. esvet says:

    I agree with my forecommenters – good Job and the best Visitor explanation I ‘ve found
    found in the net so gar.. Cheers Pankaj!

  12. shahnawaz says:

    Nice example..

  13. jagdish says:

    I have been referring to this post, whenever I need to refresh my memory on patterns. And nowhere can I find a better suitable explanation and example then here. And the examples are real world which I can relate to a project than the abstract examples elsewhere.

  14. maxat says:

    Better explanation than on Wikipedia. Good job.

  15. Dhanasekar says:

    This example is very good and self explanatory.

  16. ravi says:

    Good Job Pankaj.

  17. Abhishek says:

    Nice Explanation. I liked your work.

    Good Job!!

  18. Anjaneyulu says:

    Thanks a lot. You really made us understand this clearly.

  19. Vijay says:

    Good clarity !

  20. Nici says:

    Thanks a lot for explaining all the design patterns! Your examples helped me understand it a whole lot better. Keep up the good work!

  21. mehdi ghadimi says:

    very good. tnx

  22. Mbuku Ditutala says:

    I’ve been reading lot’s of stuffs from you, meaningly on C# Remoting, but never made any commnet. I just have to say thanks for all your efforts to make programming easy for many, like me. Keep Cool and may the almighty God bless you.

    1. Peter Yan says:

      Very nice post, I can’t love you more.

  23. Anand says:

    Very nice explanation

  24. Chavda jaydeep says:

    thnx….this is very help full for me bcz i am developing my final year project for online cloth store so….thnxxxxxxxxxxxxx

    1. Anjaiah says:

      Visitor design pattern is applicable to any Java API related class or interface

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages