Design PatternsSoftware Development

Visitor Pattern Explained – Part 1: Who is the Visitor

You may have already heard of the Visitor Pattern. You may have also used it in one of your projects. But have you ever wondered why it was called the Visitor pattern, and how is the name tied to its intended use?

I wondered that as well. After using the visitor pattern in a number of projects, I came to realize the power it brings, as well as the downsides that come with it. But one thing I could never wrap my head around is why is it called a Visitor Pattern?

This blog series will answer that question and many more:

  1. The first part (which you are reading right now) will look into who the Visitor in the visitor pattern is exactly, and provide a colorful example of the reason why it is called a Visitor.
  2. The second part (coming soon) shows a practical coding example (not the Animal – Cat – Dog examples), specifics on how to use the visitor pattern, such as how to pass arguments to the visitor, and how a visitor returns a value, as well as pros and cons of the pattern.

What exactly is the Visitor Pattern?

Before we delve into the reason why the pattern is called the Visitor Pattern, let’s see what it is. And what better place to get the definition than from the book that invented the pattern, the “Design Patterns: Elements of Reusable Object-Oriented Software, a.k.a. the book by “The Gang of Four”.

Design Patterns – image was taken from Amazon

The book documents the Visitor Pattern exceptionally well, and I highly recommend it as a necessary read for any software developer.

In the book, you can find the following definition of the Visitor pattern:

Represent an operation to be performed on the elements of an object structure.

Visitor lets you define a new Operation without changing the classes of the elements on which it operates.

Simply put, the Visitor Pattern helps us add new operations (new features) to existing classes, without changing the classes themselvesThis is helpful for a number of reasons, one of which is to avoid putting everything in one class, which breaks the Single Responsibility Principle. (You can read more about the Single Responsibility Principle on this great blog post on Rubik’s Code).

The above definition is fine and dandy, but in the past, I always had trouble connecting the definition with the name Visitor Pattern.

Why is it called a Visitor Pattern?

I don’t know about you, but when I think of a Visitor, I think of guests coming to my home for dinner. You might be wondering what do dinner guests have in common with adding new operations to existing classes?

Do guests coming for a cup of coffee represent a Visitor Pattern?

The answer is Nothing at all!

In the Visitor Pattern, do not think of visitors as guests coming to your house. Instead, think of the visitor as a handyman!

The Handyman visits your house, not for dinner, but to do one job inside your house. Which brings us back to the definition:

Represent an operation (fixes in the house) to be performed on the elements of an object structure (house).

The Handyman performs a fix in the house (fixing leaky pipes, fixing broken windows, fixing electricity or internet problems).

That covers the first part of the definition. Now for the second part:

Visitor lets you define a new Operation without changing the classes of the elements on which it operates.

This essentially means that the handyman does not need to change any critical parts of your house in order to perform the fix. In order to fix a leaky pipe, the plumber does not need to restructure your entire house. What this means in programming is that you don’t need to change the structure of the class to add a new visitor, and with that, a new operation.

Leaky pipes? Let’s restructure your house!

These examples illustrate the primary reason to use the Visitor pattern. 

Your house has all the necessary components that are of interest to a handyman (pipes, TV, windows, doors). The house does not care how the handyman performs the job, it only cares about the status of its components. The status of the components can range from the state of the pipes (leaky or working normally), state of windows (broken, cannot be opened, cannot be closed, working as intended) to the TV not being operational.

Of course, there are other examples of why a handyman can come to your house, most of which you are not aware of when you are buying a house. Which is not a problem at all, because the only thing you need when you accept a new handyman for your house is a front door!

Visitor navigating through the object structure

Let’s look at an example of how a visitor moves through the object structure to perform its operations, using the Handyman as an example.

A house consists of multiple rooms. Usually, there is a living room, a kitchen, a bathroom, and one or more bedrooms and each room has certain components. For example, the bathroom and the kitchen have pipes, while the living room has a TV, and all rooms have windows. The handyman will go through the house room by room and check only for the components they are interested in.

Let us take a plumber for example. The plumber would first enter the house, and go to the first room, the living room. Since the living room does not have any pipes, the plumber will continue to the next room, until a room with pipes is found. If the room has leaky pipes, the plumber will fix them, and then proceed to the next room, until another room with pipes has been found.

A similar example can be seen for a cable technician, who will go from room to room, but instead of looking for pipes, the technician would look for a TV, or an internet modem to fix.

A pattern can be noticed here. A handyman will examine each room if there is something of interest or something that can be fixed. This is exactly how we use visitors in the code! They go from object to object and interact with it by examining what is of interest to that visitor and performing some operation.

To illustrate this, let’s put the above example with the plumber and cable technician in the code. (Don’t worry, more complex and interesting examples will follow in Part 2). The implementation of the checks that the handyman performs is omitted to keep the example simple and to focus on the Visitor Pattern.

Conclusion

Today we have learned who exactly is the Visitor in the Visitor pattern, with a simple example illustrating that. Hopefully, you now have a better understanding of why the Visitor Pattern is called like that and the primary reason why it is used.

In the next part of this series on the Visitor Pattern, we will go through a more technical example, where we will learn some specifics on how to use the visitor pattern, such as how to pass arguments to the visitor, and how can a visitor return a value.

If you want to receive a notifaction when Part 2 comes out, as well as future posts, please click to enter your email below

Leave a Reply

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