What is the Visitor Design Pattern?

Göksu Deniz
11 min readJan 1, 2023

--

Image is created by DALL-E 2 AI

The visitor design pattern is a way of separating an algorithm from an object structure on which it operates. It involves creating a separate class for the algorithm, known as a visitor, that can be applied to elements of the object structure.

In the visitor pattern, the elements of the object structure (such as objects in a composite structure) have an accept method that takes a visitor as an argument. The accept method then calls a specific visit method on the visitor, passing itself as an argument. The visitor’s visit method can then perform the desired operation on the element.

One of the main benefits of the visitor pattern is that it allows you to add new operations to the object structure without modifying the elements of the structure itself. This makes it easier to maintain and extend the code, since you don’t have to change the element classes every time you want to add a new operation.

Imagine that you have a bunch of different shapes, like circles, squares, and triangles. You want to be able to do different things with each shape, like count how many sides it has or measure the size of its biggest angle.

To do this, we can use the visitor design pattern. We’ll start by creating a Shape class that has an Accept method. This method will take in something called a Visitor, which is a special class that knows how to do different things with the shapes.

Next, we’ll create three different types of shapes: Circle, Square, and Triangle. Each of these shapes will be a subclass of the Shape class and will have its own unique properties and methods. For example, the Circle class might have a Radius attribute and a GetArea method, while the Triangle class might have a SideLength attribute and a GetPerimeter method.

Finally, we’ll create two different types of visitors: one that knows how to count the number of sides for each shape, and another that knows how to measure the size of the biggest angle for each shape. These visitors will each be a subclass of the Visitor class and will have their own unique methods.

To use the visitor pattern, we first create some shapes and put them in a list. Then, we create a visitor and call the Accept method on each shape, passing the visitor as an argument. This will call the visitor's special method for each shape, allowing us to do different things with each shape.

using System;

abstract class Shape
{
public abstract void Accept(IVisitor visitor);
}

class Circle : Shape
{
public double Radius { get; set; }

public Circle(double radius)
{
Radius = radius;
}

public override void Accept(IVisitor visitor)
{
visitor.VisitCircle(this);
}
}

class Square : Shape
{
public double SideLength { get; set; }

public Square(double sideLength)
{
SideLength = sideLength;
}

public override void Accept(IVisitor visitor)
{
visitor.VisitSquare(this);
}
}

class Triangle : Shape
{
public double SideLength { get; set; }
public double Height { get; set; }

public Triangle(double sideLength, double height)
{
SideLength = sideLength;
Height = height;
}

public override void Accept(IVisitor visitor)
{
visitor.VisitTriangle(this);
}
}

interface IVisitor
{
void VisitCircle(Circle circle);
void VisitSquare(Square square);
void VisitTriangle(Triangle triangle);
}

class AreaVisitor : IVisitor
{
public void VisitCircle(Circle circle)
{
double area = Math.PI * circle.Radius * circle.Radius;
Console.WriteLine($"The area of the circle is {area}.");
}

public void VisitSquare(Square square)
{
double area = square.SideLength * square.SideLength;
Console.WriteLine($"The area of the square is {area}.");
}

public void VisitTriangle(Triangle triangle)
{
double area = 0.5 * triangle.SideLength * triangle.Height;
Console.WriteLine($"The area of the triangle is {area}.");
}
}

class PerimeterVisitor : IVisitor
{
public void VisitCircle(Circle circle)
{
double perimeter = 2 * Math.PI * circle.Radius;
Console.WriteLine($"The perimeter of the circle is {perimeter}.");
}

public void VisitSquare(Square square)
{
double perimeter = 4 * square.SideLength;
Console.WriteLine($"The perimeter of the square is {perimeter}.");
}

public void VisitTriangle(Triangle triangle)
{
double perimeter = 3 * triangle.SideLength;
Console.WriteLine($"The perimeter of the triangle is {perimeter}.");
}
}

And you can use it on client size like this below:

class Program
{
static void Main(string[] args)
{
Shape[] shapes = { new Circle(5), new Square(10), new Triangle(3, 4) };
IVisitor areaVisitor = new AreaVisitor();
IVisitor perimeterVisitor = new PerimeterVisitor();

foreach (Shape shape in shapes)
{
shape.Accept(areaVisitor);
shape.Accept(perimeterVisitor);
}
}
}

This code creates an array of shapes and two visitors: an AreaVisitor and a PerimeterVisitor. It then loops through the shapes and calls the Accept method on each shape, passing in each visitor in turn. This will call the appropriate visit method on the visitor for each shape, allowing the visitor to perform its specific operation on the shape.

Here are a few real-world scenarios where the visitor design pattern might be useful:

  1. In a computer graphics program, you might have a complex object structure that represents a 3D scene. The object structure might include objects such as meshes, lights, and cameras. You might want to be able to perform different operations on the objects in the scene, such as rendering the scene, calculating lighting, or exporting the scene to a file. Using the visitor pattern would allow you to define separate visitor classes for each of these operations, making it easy to add new operations or change existing ones without modifying the object structure itself.
  2. In a software development tool, you might have a large codebase with many different types of source code files, such as Java, C++, and Python. You might want to be able to perform different operations on the source code files, such as syntax highlighting, code completion, or error checking. Using the visitor pattern would allow you to define separate visitor classes for each of these operations, making it easy to add new operations or change existing ones without modifying the source code files themselves.
  3. In a network monitoring tool, you might have a complex object structure that represents the network devices and connections in a large enterprise network. You might want to be able to perform different operations on the network devices, such as collecting performance statistics, generating alerts, or visualizing the network topology. Using the visitor pattern would allow you to define separate visitor classes for each of these operations, making it easy to add new operations or change existing ones without modifying the object structure itself.

Here’s an example of how the visitor design pattern might be used in a network monitoring tool:

using System;
using System.Collections.Generic;

abstract class NetworkDevice
{
public abstract void Accept(INetworkVisitor visitor);
}

class Router : NetworkDevice
{
public string Name { get; set; }
public string IpAddress { get; set; }
public List<string> ConnectedDevices { get; set; }

public Router(string name, string ipAddress, List<string> connectedDevices)
{
Name = name;
IpAddress = ipAddress;
ConnectedDevices = connectedDevices;
}

public override void Accept(INetworkVisitor visitor)
{
visitor.VisitRouter(this);
}
}

class Switch : NetworkDevice
{
public string Name { get; set; }
public string IpAddress { get; set; }
public int PortCount { get; set; }
public List<string> ConnectedDevices { get; set; }

public Switch(string name, string ipAddress, int portCount, List<string> connectedDevices)
{
Name = name;
IpAddress = ipAddress;
PortCount = portCount;
ConnectedDevices = connectedDevices;
}

public override void Accept(INetworkVisitor visitor)
{
visitor.VisitSwitch(this);
}
}

class Computer : NetworkDevice
{
public string Name { get; set; }
public string IpAddress { get; set; }
public string MacAddress { get; set; }

public Computer(string name, string ipAddress, string macAddress)
{
Name = name;
IpAddress = ipAddress;
MacAddress = macAddress;
}

public override void Accept(INetworkVisitor visitor)
{
visitor.VisitComputer(this);
}
}

And here is the NetworkMonitor for visitor process implementations:

interface INetworkVisitor
{
void VisitRouter(Router router);
void VisitSwitch(Switch switch);
void VisitComputer(Computer computer);
}

class NetworkMonitor : INetworkVisitor
{
public Dictionary<string, double> DeviceStatistics { get; set; }

public NetworkMonitor()
{
DeviceStatistics = new Dictionary<string, double>();
}

public void VisitRouter(Router router)
{
double averageLatency = CalculateAverageLatency(router);
DeviceStatistics[router.Name] = averageLatency;
Console.WriteLine($"Router {router.Name} has an average latency of {averageLatency} milliseconds.");
}

public void VisitSwitch(Switch switch)
{
double averageLatency = CalculateAverageLatency(switch);
DeviceStatistics[switch.Name] = averageLatency;
Console.WriteLine($"Switch {switch.Name} has an average latency of {averageLatency} milliseconds.");
}

public void VisitComputer(Computer computer)
{
double averageLatency = CalculateAverageLatency(computer);
DeviceStatistics[computer.Name] = averageLatency;
Console.WriteLine($"Computer {computer.Name} has an average latency of {averageLatency} milliseconds.");
}

private double CalculateAverageLatency(NetworkDevice device)
{
// simulate calculating average latency by returning a random value
Random random = new Random();
return random.NextDouble() * 1000;
}
}

This NetworkMonitor class implements the INetworkVisitor interface and provides visit methods for each type of NetworkDevice. It also has a dictionary called DeviceStatistics that stores the average latency for each device. The Visit methods calculate the average latency for each device by calling the CalculateAverageLatency method, which simulates this calculation by returning a random value. Finally, the Visit methods print out a message indicating the average latency for each device.

Then you can use it on client side where you want:

using System;
using System.Collections.Generic;

class Program
{
static void Main(string[] args)
{
// create a list of network devices
List<NetworkDevice> devices = new List<NetworkDevice>
{
new Router("Router 1", "192.168.1.1", new List<string> { "Computer 1", "Computer 2", "Switch 1" }),
new Switch("Switch 1", "192.168.1.2", 24, new List<string> { "Computer 1", "Computer 2", "Computer 3", "Computer 4" }),
new Computer("Computer 1", "192.168.1.3", "00:11:22:33:44:55"),
new Computer("Computer 2", "192.168.1.4", "00:11:22:33:44:56"),
new Computer("Computer 3", "192.168.1.5", "00:11:22:33:44:57"),
new Computer("Computer 4", "192.168.1.6", "00:11:22:33:44:58")
};

// create a network monitor visitor
NetworkMonitor monitor = new NetworkMonitor();

// call Accept on each device, passing in the network monitor visitor
foreach (NetworkDevice device in devices)
{
device.Accept(monitor);
}

// print out the device statistics
Console.WriteLine("Device statistics:");
foreach (KeyValuePair<string, double> entry in monitor.DeviceStatistics)
{
Console.WriteLine($"{entry.Key}: {entry.Value} milliseconds");
}
}
}

This code creates a list of NetworkDevice objects and a NetworkMonitor visitor. It then calls the Accept method on each device, passing in the NetworkMonitor as an argument. This will cause the appropriate visit method on the NetworkMonitor to be called for each device, allowing the NetworkMonitor to perform its operation on the device.

The Accept method is defined in the NetworkDevice class and is implemented differently for each type of NetworkDevice. When the Accept method is called on a NetworkDevice object, it calls the appropriate visit method on the NetworkMonitor object, passing itself as an argument. For example, if the Accept method is called on a Router object, it will call the VisitRouter method on the NetworkMonitor object, passing the Router object as an argument. This allows the NetworkMonitor to access the specific properties and methods of the Router object and perform its operation on it.

Here are the steps for using the visitor design pattern in your client code:

  1. Create an object structure that contains the elements you want to operate on. In this case, the object structure would contain NetworkDevice objects such as routers, switches, and computers.
  2. Create a visitor interface that defines the operations that can be performed on the elements in the object structure. The visitor interface should define a visit method for each type of element in the object structure.
  3. Create concrete visitor classes that implement the visitor interface and provide specific implementations for the visit methods. These visitor classes will define the actual operations to be performed on the elements.
  4. In your client code, create instances of the object structure and the visitor objects.
  5. Iterate over the elements in the object structure and call the Accept method on each element, passing in the visitor object as an argument. This will cause the appropriate visit method on the visitor object to be called for each element, allowing the visitor to perform its operation on the element.

The ExpressionVisitor class in C# uses the visitor design pattern.

The ExpressionVisitor class in C# is used to traverse the nodes of an expression tree and perform operations on the nodes. It does this by defining a visit method for each type of node in the expression tree. When you create a subclass of ExpressionVisitor, you can override these visit methods to provide specific implementations for the operations you want to perform on the nodes.

The ExpressionVisitor class uses the visitor design pattern by defining an Accept method on each type of node in the expression tree. When you call the Accept method on a node, it calls the appropriate visit method on the ExpressionVisitor object, passing itself as an argument. This allows the ExpressionVisitor to access the specific properties and methods of the node and perform its operation on it.

To use ExpressionVisitor, you create a subclass and override one or more of its methods. The methods of ExpressionVisitor correspond to the different types of nodes that can appear in an expression tree. When you visit an expression tree using an ExpressionVisitor, the appropriate method for each node in the tree will be called.

Here is an example of how you might use ExpressionVisitor to print out the nodes of an expression tree:

using System;
using System.Linq.Expressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Expression<Func<int, bool>> expr = x => x > 10;
PrintVisitor visitor = new PrintVisitor();
visitor.Visit(expr);
}
}

class PrintVisitor : ExpressionVisitor
{
protected override Expression VisitBinary(BinaryExpression node)
{
Console.WriteLine("BinaryExpression: {0}", node.NodeType);
this.Visit(node.Left);
this.Visit(node.Right);
return node;
}

protected override Expression VisitConstant(ConstantExpression node)
{
Console.WriteLine("ConstantExpression: {0}", node.Value);
return node;
}

protected override Expression VisitParameter(ParameterExpression node)
{
Console.WriteLine("ParameterExpression: {0}", node.Name);
return node;
}
}
}

When you run this code, it will print out the following:

BinaryExpression: GreaterThan
ParameterExpression: x
ConstantExpression: 10

Disadvantages

There are a few potential disadvantages to using the visitor design pattern:

  1. Increased complexity: The visitor design pattern can make your code more complex, as it requires you to define a separate visitor class for each operation you want to perform on the object structure. This can make the code more difficult to understand and maintain.
  2. Limited flexibility: The visitor design pattern is best suited for operations that need to be performed on all elements of an object structure. If you need to perform different operations on different subsets of the elements, it can be more difficult to use the visitor pattern.
  3. Performance overhead: The visitor design pattern involves a lot of dynamic method dispatch, as the appropriate visit method is called on the visitor object at runtime. This can lead to a performance overhead, especially in large object structures.
  4. Limited support in some languages: Some programming languages do not have direct support for the visitor design pattern, making it more difficult to use.

That being said, the visitor design pattern can be a useful tool in certain situations, such as when you need to perform a large number of operations on an object structure and you want to keep the operations separate from the object structure itself.

In conclusion, the visitor design pattern is a way to separate an operation from an object structure on which it operates. It allows you to add new operations to the object structure without modifying the structure itself. The visitor design pattern is useful in situations where you need to perform a large number of operations on an object structure and you want to keep the operations separate from the object structure itself.

However, the visitor design pattern also has some potential disadvantages, such as increased complexity, limited flexibility, performance overhead, and limited support in some languages. It is important to consider these potential drawbacks when deciding whether to use the visitor design pattern in your code.

Overall, the visitor design pattern can be a useful tool in certain situations, but it may not be the best solution in all cases. It is important to carefully evaluate your specific requirements and consider the pros and cons of using the visitor design pattern before deciding whether to use it in your code.

Here are some useful situations where the visitor design pattern useful:

  1. When you have a complex object structure and you want to perform different operations on the elements of the structure. Using the visitor pattern allows you to separate the operations from the object structure, making it easier to add new operations without modifying the structure itself.
  2. When you want to perform an operation on a large number of different types of objects, and the operation is different for each type of object. The visitor pattern allows you to define a separate operation for each type of object, making it easy to add new types of objects and operations.
  3. When you want to perform an operation on an object structure that contains elements of different types, and the type of an element is not known until runtime. The visitor pattern allows you to define the operation for each type of element in a separate class, so you can apply the operation to the element regardless of its type.
  4. When you want to perform an operation on an object structure that changes frequently. Using the visitor pattern makes it easy to add new operations or change existing ones, since the object structure itself does not need to be modified.

Thanks for reading! If you found the article helpful, you can clap and follow. So you will notified of new articles.

# Reference

It was created with the help of ChatGPT AI.

--

--

Göksu Deniz

Software Engineer, passionate about creating efficient solutions. Skilled in mentoring teams to deliver successful projects. Always exploring new tech trends.