VISITOR 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. So using visitor pattern we can move out logics to another class.

The Visitor pattern allows the operation to be defined without changing the class of any of the objects in the collection. To accomplish this, the Visitor pattern suggests defining the operation in a separate class referred to as a visitor class. This separates the operation from the object collection that it operates on. For every new operation to be defined, a new visitor class is created. Since the operation is to be performed across a set of objects, the visitor needs a way of accessing the public members of these objects.

Following components are involved in visitor pattern-

Visitor: Declares a Visit operation for each class of ConcreteElement in the object structure. The operation’s name and signature identifies the class that sends the Visit request to the visitor. That lets the visitor determine the concrete class of the element being visited. Then the visitor can access the element directly through its particular interface.

ConcreteVisitor: Implements each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class of object in the structure. ConcreteVisitor provides the context for the algorithm and stores its local state. This state often accumulates results during the traversal of the structure.

Element: Defines an Accept operation that takes a visitor as an argument.

ConcreteElement: Implements an Accept operation that takes a visitor as an argument.

ObjectStructure:

Can enumerate its elements.

  • May provide a high-level interface to allow the visitor to visit its elements.
  • May either be a composite or a collection such as a list or a set.

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

Example:

package com.myjavablog.behavioural.visitor;

public interface ShoppingCartElement {

//accept method takes Visitor argument
public int accept(ShoppingCartVisitor visitor);
}

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

package com.myjavablog.behavioural.visitor;

public class Book implements ShoppingCartElement {

private int price;
private String isbnNumber;

public Book(int price, String isbnNumber) {
this.price = price;
this.isbnNumber = isbnNumber;
}

public int getPrice() {
return price;
}

public String getIsbnNumber() {
return isbnNumber;
}

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

package com.myjavablog.behavioural.visitor;

public class Fruit implements ShoppingCartElement {

private int pricePerKg;
private int weight;
private String name;

public Fruit(int pricePerKg, int weight, String name) {
this.pricePerKg = pricePerKg;
this.weight = weight;
this.name = name;
}

public int getPricePerKg() {
return pricePerKg;
}

public int getWeight() {
return weight;
}

public String getName() {
return name;
}

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

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

package com.myjavablog.behavioural.visitor;

public interface ShoppingCartVisitor {

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

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

package com.myjavablog.behavioural.visitor;

public class ShoppingCartVisitorImpl implements ShoppingCartVisitor {
@Override
public int visit(Book book) {
int cost = 0;
//Apply discount if book price is greator than 50
if(book.getPrice() > 50){
cost = book.getPrice() - 5;
}else{
cost = book.getPrice();
}
System.out.println("Book IBSN Number: "+ book.getIsbnNumber() +" Cost : "+ cost );
return cost;
}

@Override
public int visit(Fruit fruit) {
int cost =0;

cost = fruit.getPricePerKg() * fruit.getWeight();
System.out.println("Fruit Name: "+ fruit.getName() +" Cost: "+ cost);
return cost;
}
}

Let’s see how we can use it in client applications.

package com.myjavablog.behavioural.visitor;

public class VisitorPatternTest {

public static void main(String[] args) {

ShoppingCartElement[] items = new ShoppingCartElement[]{new Book(100, "You can win"),
new Fruit(100, 2, "Apple" ), new Book(200, "Five Point Someone"),
new Fruit(40, 4, "Gauva")};

System.out.println("Total cost: "+ VisitorPatternTest.calculatePrice(items));
}

public static int calculatePrice(ShoppingCartElement[] items){

ShoppingCartVisitor visitor = new ShoppingCartVisitorImpl();
int sum = 0;

for(ShoppingCartElement element : items){
sum = sum + element.accept(visitor);
}

return sum;
}
}

Output:

Book IBSN Number: You can win Cost : 95
Fruit Name: Apple Cost: 200
Book IBSN Number: Five Point Someone Cost : 195
Fruit Name: Gauva Cost: 160
Total cost: 650

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.

When to use the Visitor Design Pattern:

Use the Visitor pattern when:

  • An object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes.
  • Many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid “polluting” their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them.
  • The classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it’s probably better to define the operations in those classes.

Visitor Design Pattern in JDK:

  • javax.lang.model.type.TypeMirror and javax.lang.model.type.TypeVisitor
  • javax.lang.model.element.Element and javax.lang.model.element.ElementVisitor

Leave a Comment

Bitnami