Visitor

Visitor

Behavioral pattern

The Visitor design pattern is used for separating the logic and operations performed over a complex structure. On occasions, we can find data structures requiring various operations to be performed and needing new ones to be created as the application grows.

As the application grows, so does the number of operations included in the structure, making it very difficult to manage. This where the Visitor design patterns enters, it proposes the separation of these operations into independent classes called Visitors, created with a common interface and a basic structure for adding operations without having to make modifications to it.

Visitor design pattern structure
Visitor design pattern structure.

Below, the components included in this pattern are explained.

  • Client: Component interacting with the structure (element) and with the Visitor pattern, this is the one in charge of creating the visitors and sending to the element.
  • Element: It represents the root of the tree structure in which the Visitor pattern will be implemented. This object is typically an interface with accept method defined, which must be implemented by all objects in the structure.
  • ConcreteElement: It represents a child of the built structure. The entire structure may be compounded of many of these objects, and each and every one of them must have the accept method implemented.
  • IVisitor: Interface defining the structure of the visitor, it must have a method for each object of the structure (element) requiring to be analyzed.
  • ConcreteVisitor: It represents an implementation of the visitor. This implementation can perform operations over the element. It is possible to have as many ConcreteVisitor as we need for performing the various operations we need.
Visitor pattern sequence diagram
Visitor pattern sequence diagram
  1. The client creates the structure (Element).
  2. The client creates the instance of the Visitor to be used in the structure.
  3. The client executes the accept method of the structure and sends the Visitor.
  4. The Element tells the Visitor which method must be used for processing it. The Visitor must have a method for each class type included in the structure.
  5. The Visitor analyzes the Element using its visitElement method and repeats the accept method execution process over the Element's children. Again, the Visitor must have a method for each class type included in the structure.
  6. The ConcreteElementA tells the Visitor visitor which method must be used for processing it, that is visitElementA.
  7. The Visitor proceeds with the rest of the children of the Element and executes the accept method on ConcreteElementB.
  8. The ConcrteElementB tells the Visitor which method must be used for processing it, that is visitElementB.
  9. Lastly, the Visitor ends the operation performed over the structure after having traversed all the objects, providing results available for the client by request using the getResults method (this is optional, because some operations won't produce any results).

Real-world example

By implementing the Visitor design pattern, we are going to review how a plan to build a Microsoft Project type project is analyzed and evaluated to get the information we require. To that end, we will develop a work plan which includes activities and personnel forming a hierarchical structure similar to a tree.

Discover how the Visitor pattern can help us solve this problem
Discover how the Visitor pattern can help us solve this problem

About this book

Introducción a la arquitectura de software

Everything you just saw in this article is only a small part of the book Introduction to design patterns, the most complete book of design patterns in Spanish, we cover 25 design patterns along with 25 real-world projects. Forget about learning design patterns with typical Internet examples of how to make pizza, animals, and geometric shapes. I invite you to see my book:

See book
All rights reserved ©