visitor design pattern
The Visitor design pattern is a behavioral pattern that allows you to add new operations to a group of related classes without modifying their structures.
Real-World Example of Visitor Design Pattern
A simple example of the Visitor design pattern is in an online shopping cart. Imagine you have different items like books, electronics, and clothing. Each item can accept a visitor that performs actions like calculating the total price or applying discounts. This way, you can add new features without changing the item classes, making the system easier to maintain and update.
UML Class Diagram of Visitor design pattern
Components of Visitor Design Pattern
The Visitor design pattern consists of several key components that work together to enable its functionality. Here’s a breakdown of these components:
- Visitor Interface: This interface declares a visit method for each type of element in the object structure. Each method is designed to handle a specific element type.
- Concrete Visitor: This class implements the Visitor interface and provides the specific behavior for each visit method. It contains the logic for the operations that need to be performed on the elements.
- Element Interface: This interface defines an
accept
method that takes a visitor as an argument. This method allows the visitor to visit the concrete elements. - Concrete Elements: These classes implement the Element interface and represent the various types of objects in the structure. Each concrete element defines how it accepts a visitor by calling the corresponding method on the visitor.
- Object Structure: This is the collection of elements (the concrete elements) that the visitor will operate on. It often includes methods to add, remove, and retrieve elements.
Step 1: Define the Visitor interface:
public interface ShapeVisitor {
void visit(Circle circle);
void visit(Square square);
void visit(Triangle triangle);
}
Step 2: Define the Element interface:
public interface Shape {
void accept(ShapeVisitor visitor);
}
Step 3: Implement Concrete Elements:
public class Circle implements Shape {
// Circle specific properties and methods
@Override
public void accept(ShapeVisitor visitor) {
visitor.visit(this);
}
}
class Square implements Shape {
@Override
public void accept(ShapeVisitor visitor) {
visitor.visit(this);
}
}
class Triangle implements Shape {
@Override
public void accept(ShapeVisitor visitor) {
visitor.visit(this);
}
}
Step 4: Implement Concrete Visitors:
public class AreaCalculator implements ShapeVisitor {
double totalArea = 0;
@Override
public void visit(Circle circle) {
// Calculate area of circle and update totalArea
totalArea += Math.PI * Math.pow(radiusOfCircle, 2);
}
@Override
public void visit(Square square) {
// Calculate area of square and update totalArea
totalArea += Math.pow(sideOfSquare, 2);
}
@Override
public void visit(Triangle triangle) {
// Calculate area of triangle and update totalArea
totalArea += (baseOfTriangle * heightOfTriangle) / 2;
}
public double getTotalArea() {
return totalArea;
}
}
// Main class
public class Main {
public static void main(String[] args) {
List<Shape> shapes = new ArrayList<>();
shapes.add(new Circle());
shapes.add(new Square());
shapes.add(new Triangle());
AreaCalculator areaCalculator = new AreaCalculator();
for (Shape shape : shapes) {
shape.accept(areaCalculator);
}
System.out.println("Total area: " + areaCalculator.getTotalArea());
}
}
Total area: 103.53981
Comments
Post a Comment