Visitor Design Pattern

The Visitor design pattern is a behavioral design pattern in Java that is used to separate an algorithm from an object structure on which it operates. It allows new operations to be added to an existing object structure without modifying the objects themselves. This pattern is particularly useful when the object structure is complex, and different operations need to be performed on it. In this article, we will discuss the Visitor design pattern in detail.

Overview of the Visitor Design Pattern:

The Visitor design pattern is based on the following principles:

  1. Separate the algorithm from the object structure: The Visitor design pattern separates the algorithm from the object structure it operates on. The object structure is defined as a collection of objects, and the algorithm is defined as a set of operations that can be performed on the object structure. By separating the algorithm from the object structure, the pattern allows new operations to be added to the object structure without modifying the objects themselves.
  2. Dynamic binding: The Visitor design pattern uses dynamic binding to bind the algorithm to the object structure at runtime. This allows the algorithm to be selected based on the type of the object being operated on.
  3. Open-Closed Principle: The Visitor design pattern follows the Open-Closed Principle, which states that the software entities should be open for extension but closed for modification. In other words, the pattern allows new operations to be added to the object structure without modifying the existing code.

Components of the Visitor Design Pattern:

The Visitor design pattern consists of the following components:

  1. Visitor: The Visitor is an interface that defines the operations that can be performed on the object structure. It declares a visit method for each type of object in the object structure.
  2. Concrete Visitor: The Concrete Visitor implements the Visitor interface and defines the operations that can be performed on each type of object in the object structure.
  3. Element: The Element is an interface that defines the accept method. This method accepts a Visitor as a parameter and calls the visit method of the Visitor.
  4. Concrete Element: The Concrete Element implements the Element interface and defines the accept method. This method accepts a Visitor as a parameter and calls the visit method of the Visitor.
  5. Object Structure: The Object Structure is a collection of objects that can be operated on by the Visitor. It provides an interface for adding and removing objects from the collection.

Example of the Visitor Design Pattern in Java:

Let’s take an example to understand how the Visitor design pattern works in Java. Consider a scenario where we have a collection of shapes (Circle, Square, and Rectangle), and we want to calculate the area of each shape. We can implement the Visitor design pattern as follows:

Define the Visitor interface:

public interface ShapeVisitor {
public void visitCircle(Circle circle);
public void visitSquare(Square square);

public void visitRectangle(Rectangle rectangle);
}

Implement the Concrete Visitor:

public class AreaVisitor implements ShapeVisitor {
@Override
public void visitCircle(Circle circle) {
double area = Math.PI * Math.pow(circle.getRadius(), 2);
System.out.println("Area of Circle: " + area);
}

@Override
public void visitSquare(Square square) {
double area = Math.pow(square.getSide(), 2);
System.out.println("Area of Square: " + area);
}

@Override
public void visitRectangle(Rectangle rectangle) {
double area = rectangle.getLength() * rectangle.getWidth();
System.out.println("Area of Rectangle: " + area);
}
}


Define the Element interface:

public interface Shape {
public void accept(ShapeVisitor visitor);
}


Implement the Concrete Elements:

public class Circle implements Shape {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

public double getRadius() {
return radius;
}

@Override
public void accept(ShapeVisitor visitor) {
visitor.visitCircle(this);
}
}

public class Square implements Shape {
private double side;

public Square(double side) {
this.side = side;
}

public double getSide() {
return side;
}

@Override
public void accept(ShapeVisitor visitor) {
visitor.visitSquare(this);
}
}

public class Rectangle implements Shape {
private double length;
private double width;

public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

public double getLength() {
return length;
}

public double getWidth() {
return width;
}

@Override
public void accept(ShapeVisitor visitor) {
visitor.visitRectangle(this);
}
}



Define the Object Structure:

public class ShapeCollection {
private List shapes;

public ShapeCollection() {
shapes = new ArrayList();
}

public void addShape(Shape shape) {
shapes.add(shape);
}

public void removeShape(Shape shape) {
shapes.remove(shape);
}

public void accept(ShapeVisitor visitor) {
for (Shape shape : shapes) {
shape.accept(visitor);
}
}
}



Use the Object Structure and Visitor:

  public class Main {
public static void main(String[] args) {
ShapeCollection collection = new ShapeCollection();
collection.addShape(new Circle(5));
  collection.addShape(new Square(4));
  collection.addShape(new Rectangle(3, 4));

  collection.accept(new AreaVisitor());
}
}


Output:

Area of Circle: 78.53981633974483
Area of Square: 16.0
Area of Rectangle: 12.0


In this example, the AreaVisitor calculates the area of each shape by implementing the ShapeVisitor interface. The Concrete Elements (Circle, Square, and Rectangle) implement the Shape interface and define the accept method, which accepts a Visitor and calls the appropriate visit method. The ShapeCollection is the Object Structure, which is a collection of shapes that can be operated on by the Visitor. Finally, the Main class uses the Object Structure and Visitor to calculate the area of each shape.

Conclusion:

The Visitor design pattern is a powerful tool for separating an algorithm from an object structure on which it operates. It provides a way to add new operations to an existing object structure without modifying the objects themselves.