In this tutorial, you’ll learn about the Gang of Four (GoF) visitor design pattern, its definition, and its applications. You’ll also be guided on how to create a UML class diagram for the visitor pattern and how to save it as a design pattern file for future use. By the end of this tutorial, you’ll have a better understanding of how to implement the visitor design pattern in your software projects.

What is Visitor Design Pattern?

The Visitor design pattern is a behavioral pattern in object-oriented programming that allows you to separate an algorithm from an object structure on which it operates. It provides a way to add new operations to an object structure without modifying the objects themselves.

In other words, the Visitor pattern allows you to define a new operation to a group of related objects by encapsulating it in a separate object. This separate object is known as the visitor, and it can visit each object in the group, executing the appropriate operation for that object. This approach provides greater flexibility and makes it easier to add new operations to an existing object structure without modifying the objects themselves.

The Visitor pattern consists of four main components: the visitor interface, concrete visitor, element interface, and concrete element. The element interface represents the objects that will be visited, while the visitor interface declares the operations that can be performed on these objects. The concrete visitor implements the visitor interface and provides the implementation for the operations declared in the visitor interface. The concrete element implements the element interface and provides the implementation for the accept method, which accepts a visitor object and allows it to perform operations on the element.

Overall, the Visitor pattern is useful when you need to add new operations to a complex object structure without modifying the objects themselves. By encapsulating these new operations in a separate object, you can easily extend the functionality of an existing object structure and make it more flexible and maintainable.

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Patterns.
  2. Create a class diagram Visitor.
    new diagram
  3. Select Class from diagram toolbar. Click on diagram to create a class. Name it as Client.
    create client class
  4. Move the mouse cursor over the Client class, and drag out Association > Class to create an associated class Visitor.
    create visitor
  5. Right-click on Visitor, and select Model Element Properties > Abstract to set it as abstract.
    set visitor abstract
  6. Right-click on the Visitor class, and select Add > Operation from the popup menu.
    add oper to visitor
  7. Name the operation VisitConcreteElement(ConcreteElement).
  8. Right-click on VisitConcreteElement, and select Model Element Properties > Abstract to set it as abstract.
    set oper abstract
  9. Move the mouse cursor over the Visitor class, and drag out Generalization > Class to create subclasses ConcreteVisitor.
    create concrete visitor
  10. We need to make the concrete visitors inherit operations from the visitor class. Right-click on ConcreteVisitor and select Related Elements > Realize all Interfaces from the popup menu.
    realize visitor
  11. Move the mouse cursor over the Client class, and drag out Association > Class to create an associated class ObjectStructure.
    create object structure
  12. Move the mouse cursor over the ObjectStructure class, and drag out Association > Class to create an associated class Element.
    created element class
  13. Right-click on Element, and select Model Element Properties > Abstract to set it as abstract.
  14. Right-click on the Element class, and select Add > Operation from the popup menu. Name the operation Accept(Visitor).
  15. Right-click on Accept(Visitor), and select Model Element Properties > Abstract to set it as abstract. Up to now, the diagram becomes:
    element defined
  16. Move the mouse cursor over the Element class, and drag out Generalization > Class to create subclasses ConcreteElement.
    create concrete element
  17. We need to make the concrete elements inherit operations from the element class. Right-click on ConcreteElement and select Related Elements > Realize all Interfaces from the popup menu.
    realize element
  18. In practice, there may be multiple ConcreteVisitor class. To represent this, stereotype the ConcreteVisitor class as PTN Cloneable. Right-click on ConcreteVisitor class and select Stereotypes > Stereotypes… from the popup menu.
    select stereotypes
  19. In the Stereotypes tab of class specification, select PTN Cloneable and click > to assign it to the class. Click OK to confirm.
    set ptn cloneable
  20. Repeat step 18 and 19 on ConcreteElement.
    ptn cloneable
  21. There maybe multiple operations in VisitorElement and ConcreteElement. To represent this, stereotype them as PTN Members Creatable. Repeat steps 18 and 19 to stereotype VisitorElement and ConcreteElement as PTN Members Creatable. Up to now, the pattern should look like this:
    pattern modeled

Saving the Pattern

  1. Select all classes on the class diagram.
    select all classes
  2. Right-click on the selection and select Define Design Pattern… from the popup menu.
    define design pattern
  3. In the Define Design Pattern dialog box, specify the pattern name Visitor. Keep the file name as is. Click OK to proceed.
    name pattern

Applying Design Pattern on Class Diagram

In this section, we will try to make use of the visitor pattern to model the visiting of elements in a room.

  1. Create a new project My Room.
  2. Create a class diagram Domain Model.
  3. Right-click on the class diagram and select Utilities > Apply Design Pattern… from the popup menu.
    apply design pattern
  4. In the Design Pattern dialog box, select Visitor from the list of patterns.
    select visitor
  5. Click on ObjectStructure in the overview.
    select object structure
  6. Rename it to RoomElements at the bottom pane.
    rename object structure
  7. Select Element in overview. At the bottom pane, rename it to RoomElement. Rename parameter Visitor in Accept() to RoomElementVisitor.
    rename element
  8. Select ConcreteElement in overview. At the bottom pane, rename it to Chair. Rename parameter Visitor in Accept() to RoomElementVisitor.
    rename concrete element
  9. We need 2 more concrete elements for bed and table. Keep ConcreteElement selected, click on the + button and select Clone… from the popup menu.
    clone
  10. Enter 2 to be the number of classes to clone. Click OK to confirm.
    clone count
  11. At the bottom pane, rename ConcreteElement2 and ConcreteElement3 to Bed and Table. Rename parameter Visitor to RoomElementVisitor.
    rename concrete elements
  12. Select Visitor in overview. At the bottom pane, rename it to RoomElementVisitor. Rename operation VisitConcreteElement to VisitChair, and parameter ConcreteElement to Chair.
    rename visitor
  13. We need 2 more operations for visiting bed and table. Keep Visitor selected, click on the + button and select New Operation… from the popup menu.
    new oper
  14. In the Operation Specification dialog box, name the operation VisitBed.
    name oper
  15. Open the Parameters tab.
    open param tab
  16. Click Add… at the bottom, and create parameter Chair in Parameter Specification dialog box. Click OK to go back to the Operation Specification dialog box.
    enter param
  17. In the General page, check Abstract.
    set oper abstract
  18. Click OK to confirm editing.
  19. Repeat steps 13 to 18 to create one more abstract operation VisitTable which have Table as parameter.
    opers added to visitor
  20. Select ConcreteVisitor in overview. At the bottom pane, rename it to RoomElementPaintVisitor. Rename operation VisitConcreteElement to VisitChair, and parameter ConcreteElement to Chair.
    rename concrete visitor
  21. We need one more visitor for printing elements’ information. Keep ConcreteVisitor selected, click on the + button and select Clone… from the popup menu.
    select clone
  22. Enter 1 to be the number of classes to clone. Click OK to confirm.
    clone count
  23. At the bottom pane, rename ConcreteVisitor2 to RoomElementInfoVisitor. Rename operation VisitConcreteElement to VisitChair, and parameter ConcreteElement to Chair.
    rename concrete visitor 20
  24. Click OK to confirm editing and apply the pattern to diagram.
  25. Tidy up the diagram. It should become:
    result

Resources

  1. Design Patterns.vpp
  2. Visitor.pat

Related Links

Leave a Comment

Your email address will not be published.