Behavioural Design Pattern: Visitor Design Pattern

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:01:00

Define Visitor Design Pattern

This pattern is used to construct and execute new operations on a collection of objects without modifying its structure or classes. This style allows for loose coupling and the inclusion of additional processes without affecting the current structure.

Examples of Visitor Patterns

Examples of the visitor pattern include:

  • Add functions to an object dynamically.
  • Separate Objects from Functions.

Visitor Pattern with UML diagram

The classes, interfaces, and objects in the above UML class diagram are defined as follows.

  • Client: This class has access to the data structure objects and can direct them to accept a Visitor and perform the necessary operations.
  • ObjectStructure: This class contains all of the elements that can be used by visitors.
  • Element: This interface defines the Accept operation.
  • ConcreteElementA/B: These classes implement the Element interface and store the actual data.
  • Visitor: This interface defines the Visit procedures for certain visitors.
  • ConcreteVisitorA/B: These are subclasses that implement the Visitor interface.

When should you use the visitor design pattern?

The visitor design pattern is applicable in the following situations:

  • Many unrelated operations can be performed on an object structure.
  • An object structure cannot change, but you must perform new actions on it.
  • The operations must occur on the concrete classes of an object structure.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this