In this post, We will talk and learn about the Visitor Design Pattern in Java.
Key Points About Visitor Design Pattern :
- Visitor Design Pattern falls under behavioral design pattern.
- This Pattern is mainly used when we have to perform an operation on a group of similar kinds of Objects. This Pattern helps us to move the operational logic from the objects to another class.
- The main benefit of using this pattern is that if the logic of operation changes then we need to make a change only in the visitor implementation rather than doing modification in all the classes.
- The second benefit is that adding a new class to the system is easy, it will require change only in visitor interface and implementation and existing classes will not be affected.
Visitor Design Pattern in JDK:
- javax.lang.model.element.AnnotationValue and AnnotationValueVisitor
- javax.lang.model.element.Element and ElementVisitor
- javax.lang.model.type.TypeMirror and TypeVisitor
- java.nio.file.FileVisitor and SimpleFileVisitor
Now Let’s move towards the implementation of the Visitor Design Pattern.
Below is the complete source code:
Item.java
1 2 3 4 5 6 7 8 9 10 |
package com.kkjavatutorials.item; import com.kkjavatutorials.visitor.ShoppingCartVisitor; /** * @author KK JavaTutorials *Ccntact for an Item */ public interface Item { public double accept(ShoppingCartVisitor visitor); } |
Book.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.kkjavatutorials.item.impl; import com.kkjavatutorials.item.Item; import com.kkjavatutorials.visitor.ShoppingCartVisitor; /** * @author KK JavaTutorials *Model class which hold book item information */ public class Book implements Item { private String name; private String isbnNumber; private double price; public Book(String name, String isbnNumber, double price) { super(); this.name = name; this.isbnNumber = isbnNumber; this.price = price; } public String getName() { return name; } public String getIsbnNumber() { return isbnNumber; } public double getPrice() { return price; } @Override public double accept(ShoppingCartVisitor visitor) { return visitor.visit(this); } } |
Fruit.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.kkjavatutorials.item.impl; import com.kkjavatutorials.item.Item; import com.kkjavatutorials.visitor.ShoppingCartVisitor; /** * @author KK JavaTutorials *Model class which hold Fruit item information */ public class Fruit implements Item { private String name; private int weight; private double pricePerKg; public Fruit(String name, int weight, double pricePerKg) { super(); this.name = name; this.weight = weight; this.pricePerKg = pricePerKg; } public String getName() { return name; } public int getWeight() { return weight; } public double getPricePerKg() { return pricePerKg; } @Override public double accept(ShoppingCartVisitor visitor) { return visitor.visit(this); } } |
ShoppingCartVisitor.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.kkjavatutorials.visitor; import com.kkjavatutorials.item.impl.Book; import com.kkjavatutorials.item.impl.Fruit; /** * @author KK JavaTutorials * Contract for ShoppingCartVisitor * this API has to update for any new Item */ public interface ShoppingCartVisitor { public double visit(Book book); public double visit(Fruit fruit); } |
ShoppingCartVisitorImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.kkjavatutorials.visitor; import com.kkjavatutorials.item.impl.Book; import com.kkjavatutorials.item.impl.Fruit; /** * @author KK JavaTutorials *Overridden visit methods for all different Items */ public class ShoppingCartVisitorImpl implements ShoppingCartVisitor { @Override public double visit(Book book) { double cost = 0.0; if(book.getPrice() > 500) { cost = book.getPrice()-100; }else { cost = book.getPrice(); } System.out.println(" BooK:"+book.getName() +", Book ISBN:"+book.getIsbnNumber()+", Cost :"+cost); return cost; } @Override public double visit(Fruit fruit) { double cost = fruit.getPricePerKg()*fruit.getWeight(); System.out.println("Fruit :"+fruit.getName() + ", cost :"+cost); return cost; } } |
VisitorTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.kkjavatutorials.client; import com.kkjavatutorials.item.Item; import com.kkjavatutorials.item.impl.Book; import com.kkjavatutorials.item.impl.Fruit; import com.kkjavatutorials.visitor.ShoppingCartVisitor; import com.kkjavatutorials.visitor.ShoppingCartVisitorImpl; /** * @author KK JavaTurrials *Visitor client program */ public class VisitorTest { public static void main(String[] args) { //Add as much as Items you want Item items[] = new Item[] { new Book("Core Java", "8989898", 450.0), new Book("Hibernate", "8980998", 700.0), new Fruit("Mango", 2, 100.0), new Fruit("Apple", 3, 200.0) }; //Calculate total cost of all Items double totalCost = calculateTotalCost(items); //Display total cost System.out.println("Total Cost ::"+totalCost); } private static double calculateTotalCost(Item[] items) { ShoppingCartVisitor shoppingCartVisitor = new ShoppingCartVisitorImpl(); double totalCost = 0.0; for (Item item : items) { totalCost += item.accept(shoppingCartVisitor); } return totalCost; } } |
Output of this client Program:
BooK:Core Java, Book ISBN:8989898, Cost :450.0
BooK:Hibernate, Book ISBN:8980998, Cost :600.0
Fruit :Mango, cost :200.0
Fruit :Apple, cost :600.0
Total Cost ::1850.0
You May Also Like:
- State Design Pattern in Java
- Command Design Pattern in Java
- Observer Design Pattern in Java
- Strategy Design Pattern in java
- Mediator Design Pattern in Java
- Chain of Responsibility Design Pattern in Java
- Template Design Pattern in Java
- Bridge Design Pattern in Java.
- Flyweight Design Pattern in Java
- Composite Design Pattern in Java
- Proxy Design Pattern in Java
- Facade Design Pattern in Java
- Decorator Design Pattern in Java
- Adapter Design Pattern in Java
- Builder Pattern in Java
- Abstract Factory Pattern in Java
- Factory design pattern in java
- Prototype Pattern in Java
- Singleton Design Pattern in java
That’s all about the Visitor Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.