Visitor pattern is underrated

iamprovidence
3 min readSep 6, 2022

--

There are simple patterns like Singleton. There are complicated like Mediator. But there are also badly explained patterns like Visitor.

All explanation I came across usually was overwhelmed with unnecessary details or explain it in unrealistic circumstances.

Today, I want to have a look at a school example and see how it can benefit from Visitor pattern.

Imagine we have next requirements:

Write a library with hierarchy of shapes. There should be Square and Circle. It should also be possible to calculate Area for those. Consider, it is not expected for hierarchy to grow but new operation could be added later.

That sounds simple, isn’t it?

The naive implementation would look follow:

After all, that what we were always taught. It even allows us to use polymorphic reference:

But in a few days, as we were expected, the requirement has changed.

Update existing code to calculate Perimeter.

Don’t look at me this way. I warn you that’s gonna happen.

Sure, in our fake example, it is not a problem at all. Update an interface and all derived classes — a piece of cake. But let's look wider. In real word, you would have more than two classes in hierarchy. There will be like twenty or even more. And that's where shit got real.

Seems like our architecture need tiny enhancement. So we can rewrite it with little help from Visitor.

Notice how both Square and Circle no longer implement GetArea. Instead, it has been moved to Visitor. And the only thing our shapes know how to do is to accept our visitor.

The usage is quite similar:

Now about a change to requirement. We don't need to update the entire hierarchy anymore. Adding a calculation of perimeter is just a matter of implementing a single class:

Operation logic is encapsulated in a single class. Moreover, it also corresponds to Open/Close principle. We don’t edit any code, just add a new one.

Fascinating, isn’t it?

But do not haste with rewriting everything. There is a reverse side of a medal. What if we need to add another shape, like Triangle. That would mean to update every visitor. And again, in real world it could be still that pain in the ass.

Shame we cannot achieve expanding hierarchy and operation together 😟 Maybe somewhere in future we will find a way to do it, but for now we already have learned a lot, so let’s just summarize.

Show me what you got!

Visitor helps to define a new operation for some hierarchy of classes without changing them

It is good when:

  • new operations need to be added frequently
  • the hierarchy of classes is known and not expected to change

And vice versa. Visitor is bad when:

  • new operations need to be added rarely
  • the hierarchy of classes is not known and expected to change

Clap this article if you like it 👏
Share it with friends 🙋‍♂
Support me with link below ☕️
And follow to get more stories about design patterns ✅

--

--

iamprovidence

👨🏼‍💻 Full Stack Dev writing about software architecture, patterns and other programming stuff https://www.buymeacoffee.com/iamprovidence