Please enable Javascript to correctly display the contents on Dot Net Tricks!

Visitor Design Pattern - C#

 Print 
  Author : Shailendra Chauhan
Posted On : 24 Feb 2017
Total Views : 12,832   
Updated On : 24 Feb 2017
 

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?

This pattern 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 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 class that holds all the elements which can be used by visitors.

  3. Element

    This is an interface that specify 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 specify the Visit operations for concrete visitors.

  6. ConcreteVisitorA/B

    These are sub classes 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.

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.



ABOUT AUTHOR

Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
 
COMMENTS
20 JAN
ASP.NET Core with Angular4 (Online)
07:00 AM-09:00 AM IST / 07:30 PM Central Time
20 JAN
ASP.NET MVC with Angular (Classroom)
05:00 PM to 07:00 PM IST (+5:30 GMT)
20 JAN
ASP.NET MVC with Angular4 (Online)
03:00 pm to 05:00 pm
20 JAN
Angular2+4 with Firebase (Classroom)
03:00 PM to 05:00 PM IST (+5:30 GMT)
16 JAN
Developing Microsoft Azure Solutions (Online)
07:00 AM-09:00 AM IST/ 07:30 PM -09:30 PM CST
16 JAN
React with Redux (Online)
07:00 AM-08:30 AM IST
15 JAN
Big Data Hadoop Developer (Online)
05:00 PM to 06:30Pm IST / 06:30AM to 08:00AM CST
8 JAN
ASP.NET Core with Angular4 (Online)
09:00 Pm to 11:00 Pm IST (+5:30Gmt)
8 JAN
Big Data Hadoop Developer (Online)
07:00 AM - 08:30 AM IST(+5:30 GMT)
16 DEC
ASP.NET MVC with Angular4 (Classroom)
10:00 AM - 01::00 PM IST(+5:30 GMT)
12 DEC
ASP.NET MVC with Angular4 (Online)
09:00 PM - 11:00 PM IST(+5:30 GMT)
2 DEC
Developing Microsoft Azure Solutions (Online)
07:00 AM-09:00 AM IST / 08:30 PM Central Time
2 DEC
Angular2+4 with Firebase (Online)
10:30 AM-12:30 PM IST(+5.30 GMT)
27 NOV
ASP.NET Core (Online)
07:00 AM to 09:00 AM IST (+5:30 GMT)
11 NOV
ASP.NET MVC (Online)
08:00 AM-10:00 AM IST / 09:30 PM To 11:30 PM(CST)
SUBSCRIBE TO OUR YOUTUBE CHANNEL
 
 
 
LIKE US ON FACEBOOK
 
+