Behavioral Design Patterns provide solution for the better interaction between objects and how to provide lose coupling and flexibility to extend easily. Following design patterns come under this category.
Visitor pattern lets you add further operations to objects without having to modify them.
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.
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.
When to use the Visitor design pattern
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.
Java
To implement visitor pattern, first of all we will create different type of items (Elements) to be used in shopping cart.
File ItemElement.java.
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.
File Book.java.
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); } }
File Fruit.java.
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.
File ShoppingCartVisitor.java.
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.
File ShoppingCartVisitorImpl.java.
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.
File ShoppingCartClient.java.
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; } }
Python 3
import abc class ItemElement(metaclass=abc.ABCMeta): @abc.abstractmethod def accept(self): pass class ShoppingCartVisitor(metaclass=abc.ABCMeta): @abc.abstractmethod def visit(self, item): pass class Book(ItemElement): def __init__(self, cost, isbn): self.price = cost self.isbn = isbn def get_price(self): return self.price def get_isbn(self): return self.isbn def accept(self, visitor): return visitor.visit(self) class Fruit(ItemElement): def __init__(self, price, wt, nm): self.price = price self.weight = wt self.name = nm def get_price(self): return self.price def get_weight(self): return self.weight def get_name(self): return self.name def accept(self, visitor): return visitor.visit(self) class ShoppingCartVisitorImpl(ShoppingCartVisitor): def visit(self, item): if isinstance(item, Book): cost = 0 #apply 5$ discount if book price is greater than 50 if item.get_price() > 50: cost = item.get_price() - 5 else: cost = item.get_price() print("Book ISBN:: {} cost = {}".format(item.get_isbn(), cost)) return cost elif isinstance(item, Fruit): cost = item.get_price() * item.get_weight() print("{} cost = {}".format(item.get_name(), cost)) return cost def calculate_price(items): visitor = ShoppingCartVisitorImpl() sum = 0 for item in items: sum = sum + item.accept(visitor) return sum if __name__ == '__main__': items = [ Book(20, "1234"), Book(100, "5678"), Fruit(10, 2, "Banana"), Fruit(5, 5, "Apple") ] total = calculate_price(items) print("Total Cost = {}".format(total))