CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns
    Creational Patterns
    Structural Patterns
    Behavioral Patterns
        Chain of Responsibility
        Command
        Interpreter
        Iterator
        Mediator
        Memento
        Observer
        State
        Strategy
        Template Method
        Visitor


Visitor Design Pattern


Visitor design pattern is used to perform operation on elements of an object structure.


Intent
  • Represent an operation to be performed 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.
The intent is to represent the operation of element of object structure by visitor.

Problem
  • An object structure contains many objects. There is a need to perform different operations on these objects. Doing these operations in their classes will add complexity.
Adding different and unrelated operation in class will lead to confusion and will make it complex.

Solution
  • Have a separate object for operations of an element of object structure. Call the appropriate concrete object operation of that element when you traverse the elements.
So there will be visit operation for each element of object structure in separate class. While visiting the element the appropriate operation will be performed.

Where it is applicable?
  • An object structure has multiple objects and need to visit and apply the operations on it.
Structure

Visitor Design Pattern

Participant classes
  • Visitor class provides interface for visit operation for each element of object structure.
  • Concrete classes ConcreteVisitor1 and ConcreteVisitor2 implement the methods for the interface provided by abstract class Visitor. These methods have element instance as parameter.
  • Element class is part of ObjectStructure. It has method Accept() which gets Visitor instance as parameter.
  • The concrete class ConcreteElement class implements the method Accept() which in turn calls the appropriate Visitor method.
  • ObjectStructure may contain multiple elements. Visitor visits the elements of ObjectStructure.
How they work together?
  • The client creates the concrete visitor. While traversing the element of object structure it calls the element Accept() method with visitor as parameter. Inside Accept() method it uses visitor to call visit operation for that element with element as parameter. Inside the visit element operation it does the appropriate work applicable for that operation.
  • So here the operation is performed on elements of ObjectStructure. This provides flexibility to add new operation by adding a new visitor. It is assumed that object class structure will not be changed much. Adding a new element is complicated and will require adding of new abstract operation in visitor class and its implementation in concrete classes. So it is good to use visitor pattern when object structure is stable and addition or modification is required on operations.
Example

Implementation Code

C++   Visitor Pattern in C++   Example in C++
C#   Visitor Pattern in C#   Example in C#
Java   Visitor Pattern in Java   Example in Java
Python   Visitor Pattern in Python   Example in Python
JavaScript   Visitor Pattern in JavaScript   Example in JavaScript
PHP   Visitor Pattern in PHP   Example in PHP
Ruby   Visitor Pattern in Ruby   Example in Ruby
Swift   Visitor Pattern in Swift   Example in Swift
Objective-C   Visitor Pattern in Objective-C   Example in Objective-C



Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books "C In Depth", "Data Structures Through C In Depth". He has 18+ years experience in industry and worked on architecture and design of multiple products. This article is from his courses on design patterns.







Yearly Subscription


Design Patterns in Swift


Advanced Data Structures in C#

Explore

Courses

Subscription

Knowledge Hub

Design Patterns

Tech Articles

About Us

Overview

Contact Us

CourseGalaxy

CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap between education and industry and provide the training on specific problems of industry to enhance the skills of Engineers in industry to do their job appropriately.

Learn From Us

2018 CourseGalaxy