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
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.
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.
- The client creates the structure (Element).
- The client creates the instance of the Visitor to be used in the structure.
- The client executes the accept method of the structure and sends the Visitor.
- 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.
- 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.
- The ConcreteElementA tells the Visitor visitor which method must be used for processing it, that is visitElementA.
- The Visitor proceeds with the rest of the children of the Element and executes the accept method on ConcreteElementB.
- The ConcrteElementB tells the Visitor which method must be used for processing it, that is visitElementB.
- 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).
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.