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 : 9,251   
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
14 OCT
Angular2 and Angular4 (Online)
03:00 PM-05:00 PM IST (+5.30GMT)
12 OCT
ASP.NET Core (Online)
09:00 PM - 11:00 PM IST(+5:30 GMT)
10 OCT
Microsoft Azure Infrastructure Solutions (Online)
08:00 AM-09:30 AM IST / 09:30 PM -11:00 PM CST
30 SEP
Angular2 and Angular4 (Classroom)
08:30 AM-11:30 AM IST
20 SEP
MEAN Stack (Online)
07:00 AM-09:00 AM IST
20 SEP
ASP.NET MVC with Angular4 (Online)
9:00PM- 11:00PM IST(+5:30GMT)
16 SEP
Angular2 and Angular4 (Online)
08:00 AM-10:00 AM IST(+5.30 GMT)
22 AUG
ASP.NET Core with Angular4 (Online)
07:00 AM - 9:00 AM IST(+5:30 GMT)
LIKE US ON FACEBOOK
 
+