👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Behavioral Design Pattern - Visitor

Behavioral Design Pattern - Visitor

Design Pattern

11 Articles

Improve

Table of Contents

  1. What we gonna do?
  2. Why we gonna do?
  3. How we gonna do?
  4. Summary

What we gonna do?

The Visitor pattern is a behavioral design pattern that represents an operation performed on elements of an object structure. It allows adding new operations without altering the existing classes of these elements. For example, in an Amazon order system, FreeUser pays a ₹40 shipping cost, while PrimeUser has zero shipping cost. The Visitor pattern helps apply such behavior changes flexibly across different user types.

Why we gonna do?

The Visitor pattern is used when:

  • Object structures contain multiple classes with distinct interfaces.
  • New operations need to be added frequently without altering the existing structure.
  • You want to avoid violating the Single Responsibility Principle by not mixing unrelated behaviors into one class.
  • Accumulating information or performing distinct operations across different object types is required.

Benefits:

  1. Open/Closed Principle: New operations can be added without modifying existing classes.
  2. Single Responsibility Principle: Separates behaviors from object structures.
  3. Flexibility: Allows adding new operations without altering existing classes.

Example Scenario: In Amazon, shipping costs differ for FreeUsers and PrimeUsers. Instead of modifying user classes every time shipping logic changes, use the Visitor Pattern.

How we gonna do?

Structure

The Visitor pattern consists of:

  • Element Interface: Declares an Accept method to accept a visitor.
  • Concrete Elements: Classes like FreeUser and PrimeUser that implement the element interface.
  • Visitor Interface: Declares methods to visit each concrete element.
  • Concrete Visitor: Implements visitor methods (e.g., calculates delivery cost).
  • Object Structure: Manages elements and allows visitor traversal.

Implementation

Example: Delivery Cost Calculation

  • Elements: FreeUser (₹40 cost) and PrimeUser (₹0 cost).
  • Visitor: ShippingCostVisitor calculates and assigns costs.
  • Object Structure: A container holds users and applies the visitor to calculate costs.

Steps:

  1. Define an IElement interface with an Accept method.
  2. Create FreeUser and PrimeUser implementing IElement.
  3. Define an IVisitor interface with VisitFreeUser and VisitPrimeUser.
  4. Implement a ShippingCostVisitor to calculate costs.
  5. Create a container to hold users and apply the visitor.

Code Sample - Visitor Pattern Code Sample

Demo Space

Simplified Interface

The visitor interface can have a single Visit method accepting a general element interface. This reduces the need for updates when new elements are added but requires type checking within the visitor.

Summary

The Visitor pattern separates behaviors from object structures, adhering to the Open/Closed and Single Responsibility principles. It simplifies adding operations like shipping cost calculations (e.g., ₹40 for FreeUser, ₹0 for PrimeUser) but can introduce complexity when new elements are added or when accessing private data. Related patterns include Composite (manages object structures) and Iterator (traverses structures while applying logic).

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • Design Pattern
  • Behavioral
  • Visitor