
Episode 23 · Season 5
Software Design - The Visitor Pattern
About this episode
The visitor pattern is the last of the behavioral ones for us to review. This is going to feel similar to some others that allow us to abstract functionality such as a command. This one provides us with a way to group functionality and avoids duplication or even sprawl of our code.
The Visitor Pattern Defined
As always, we will start with the "Gang of Four" intent to set the stage for our discussion.
"Represent an operation to be perfomed 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, this pattern allows us to abstract and group functions that we will be performed on a collection of objects. It is almost like a compliment to an iterator. Instead of giving us objects from a collection (as an iterator does), this one goes to each object in the group. At each stop along the way, it performs an action or actions. A simple example is a print method. You can put a print method on every object and use an iterator to cycle through a collection so you can call that method. On the other hand, you can define the print method on the visitor object and give it a collection of objects to traverse and execute the action.
Applying The Pattern
This pattern ends up producing two potential hierarchies. One is to describe the node interface (the objects we will perform actions on). The other is the visitor hierarchy. A node can start as an interface with a way to progress to a next node. Then the visitor can perform an action or more as well as varied actions.
Java, PHP, C#, etc.
This pattern sticks to the core features of a language. All you need to implement this is a base class and inheritance. Therefore, the only differences among languages are going to be minor syntax variations to note what needs to be implemented or overridden in the child classes.
More from Develpreneur: Become a Better Developer and Entrepreneur
A handful of people love your MVP. Their suggestions keep coming, and AI helps you turn those requests into working features quickly. The product is growing. Whether it is growing in the right directi…
Your team can generate an interface faster than ever. Meanwhile, customers are still staring at confusing error messages, waiting for pages to load, and trying to figure out what happens after they cl…
After a year of deadlines, long hours, and projects that seemed determined to last forever, Thanksgiving offers a chance to pause and enjoy what all that effort has made possible. In this Thanksgiving…
The holiday is almost here, but your brain is still at work. There is something to finish, something to check, and probably something you could squeeze in before everyone arrives. Closing the laptop s…
Building a successful business requires more than developing a great product. As companies grow, they face new challenges involving customer expectations, operational efficiency, financial decisions,…