Visitor pattern in Java and Python Development 24.02.2017

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.

dp_visitor.png

Following components are involved in Visitor pattern.

  • Visitor. This is an interface or an abstract class used to declare the visit operations for all the types of visitable classes. Usually the name of the operation is the same and the operations are differentiated by the method signature. The input object type decides which of the method is called.
  • ConcreteVisitor. For each type of visitor all the visit methods, declared in abstract visitor, must be implemented. Each Visitor will be responsible for different operations. When a new visitor is defined it has to be passed to the object structure.
  • Visitable is an abstraction which declares the accept operation. This is the entry point which enables an object to be "visited" by the visitor object. Each object from a collection should implement this abstraction in order to be able to be visited.
  • ConcreteVisitable. Those classes implements the Visitable interface or class and defines the accept operation. The visitor object is passed to this object using the accept operation.
  • ObjectStructure. This is a class containing all the objects that can be visited. It offers a mechanism to iterate through all the elements. This structure is not necessarily a collection. In can be a complex structure, such as a composite object.

When to use the Visitor design pattern

  • 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.

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))