Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Visitor Design Pattern

Visitor Design Pattern

23 Aug 2022
Intermediate
9.62K Views
3 min read
Learn via Video Course & by Doing Hands-on Labs

⭐ .NET Design Patterns Course: Design Patterns in C# Online Training

Visitor Design Pattern falls under Behavioral Pattern of Gang of Four (GOF) Design Patterns in .Net. The command pattern is commonly used in the menu systems of many applications such as Editor, IDE, etc. In this article, I would like to share what is visitor pattern and how is it work?

What is Visitor Design pattern?

Visitor Design is used to create and perform new operations onto a set of objects without changing the object structure or classes. This pattern enables loose coupling and the addition of new operations without changing the existing structure.

Visitor Design Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Visitor Design Pattern is given below:

Visitor Design Pattern C#

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

  1. Client

    This is a class that has access to the data structure objects and can instruct them to accept a Visitor to perform the appropriate operations.

  2. ObjectStructure

    This is a class that holds all the elements which can be used by visitors.

  3. Element

    This is an interface that specifies the Accept operation.

  4. ConcreteElementA/B

    These are classes which implement the Element interface and holds the real information.

  5. Visitor

    This is an interface that specifies the Visit operations for concrete visitors.

  6. ConcreteVisitorA/B

    These are subclasses which implement the Visitor interface.

C# - Implementation Code

public class ObjectStructure
{
 public List Elements { get; private set; }

 public ObjectStructure()
 {
 Elements = new List();
 }

 public void Accept(Visitor visitor)
 {
 foreach (Element element in Elements)
 {
 element.Accept(visitor);
 }
 }
}

public interface Element
{
 void Accept(Visitor visitor);
}

public class ConcreteElementA : Element
{
 public void Accept(Visitor visitor)
 {
 visitor.Visit(this);
 }

 public string Name { get; set; }
}
public class ConcreteElementB : Element
{
 public void Accept(Visitor visitor)
 {
 visitor.Visit(this);
 }

 public string Title { get; set; }
}

public interface Visitor
{
 void Visit(ConcreteElementA element);

 void Visit(ConcreteElementB element);
}

public class ConcreteVisitorA : Visitor
{
 public void Visit(ConcreteElementA element)
 {
 Console.WriteLine("VisitorA visited ElementA : {0}", element.Name);
 }

 public void Visit(ConcreteElementB element)
 {
 Console.WriteLine("VisitorA visited ElementB : {0}", element.Title);
 }
}
public class ConcreteVisitorB : Visitor
{
 public void Visit(ConcreteElementA element)
 {
 Console.WriteLine("VisitorB visited ElementA : {0}", element.Name);
 }

 public void Visit(ConcreteElementB element)
 {
 Console.WriteLine("VisitorB visited ElementB : {0}", element.Title);
 }
}


Real Life Example:

Real Life Example of Visitor Design Pattern C#

When to use it?

  1. An object structure has many unrelated operations to perform on it.

  2. An object structure cannot change but you need to perform new operations on it.

  3. The operations need to perform on the concrete classes of an object structure.

Read More Articles Related to Design patterns
What do you think?

I hope you will enjoy the Visitor Design Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this