The Visitor Design Pattern

The Visitor pattern lets you add further operations to objects without having to modify the classes of the objects you’re working with.

Use the Visitor pattern when you need to add further operations to objects without having to modify them. It’s especially useful when dealing with a structure of heterogeneous classes and you need to perform operations that depend on the concrete classes.

  • TypeBehavioral
  • Time ComplexityO(n) Linear Time – The time taken grows linearly with the size of the input. Examples include simple search algorithms.
  • Efficiency5
  • Learning Effort

    • Average6
    • Conceptual8
    • Debug and Maintain6
    • Implementation6
    • Prerequisites7
    • Versatility5

Things you should know before you start

  • Polymorphism Understanding of polymorphism, as the Visitor pattern uses polymorphism to perform operations based on concrete classes.

Minimal Example

Performing rendering operations on a set of geometric shapes like circles and rectangles without modifying their classes.

interface Shape {
  accept(visitor: ShapeVisitor): void;
}
const shapes: Shape[] = [new Circle(), new Rectangle()];
const renderer = new ShapeRenderer();
shapes.forEach(shape => shape.accept(renderer));

What’s happening in thie example

In this example, the `Shape` serves as the Element, and `ShapeRenderer` is a Visitor. The `clientCode` shows how to use the Visitor pattern to perform rendering operations on different shapes.

How the Visitor Works

The Visitor pattern is useful for separating algorithms from the objects on which they operate.

It allows you to add operations to objects without modifying their structure, adhering to the Open/Closed Principle.

Entities in the Visitor Design Pattern

  • ElementThe Element interface declares an ‘accept’ method that takes a visitor object as an argument.

    • Element has a one-to-many uses relationship with Visitor

What to watch out for when using or considering this design pattern.

Avoid using the Visitor pattern if the class hierarchies you’re working with are not stable, as adding new concrete classes would require changing the visitor interface and all of its implementations.

Design patterns often confused with the Visitor Design Pattern

The following design patterns are often confused with the Visitor
for various resons.

If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.

  • Composite – The Composite pattern composes objects into tree structures to represent part-whole hierarchies.

    The Composite pattern is similar to the Visitor in the following ways.

    • Both patterns are often used together as they are both useful for tree-like structures.
    • Both are behavioral design patterns.
  • Here are some notable differences with the Visitor pattern.

    • Visitor is used for adding new operations, while Composite is used for treating both individual objects and composites uniformly.
  • Adapter – The Adapter pattern allows incompatible interfaces to work together.

    The Adapter pattern is similar to the Visitor in the following ways.

    • Both patterns allow for greater flexibility and decoupling.
    • Both can be used to extend functionalities.
  • Here are some notable differences with the Visitor pattern.

    • Visitor is aimed at executing an operation over a set of elements, while Adapter is aimed at making one interface look like another.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top