Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Visiting the Visitor Pattern

Posted by Paul on November 19, 2011

Visitor Pattern is one among the 23 design patterns. It is less known but holds a good idea for OOPs enthusiasts.


You are having a list of objects with the following class structure:

public class Member


public string Name;

public DateTime DateOfBirth;


You need to do an operation of selecting all Member having age greater than 18.

One way of the solution is add a new property called IsAboveEighteen and set the value by iterating over the items and comparing with current date. But this requires more processing.

We can achieve the same using Visitor pattern by adding a property which operations on the existing properties and returns the value.


“Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.”


Introducing a new property named IsAboveEighteen and in the getter implement the code to read DateOfBirth property to calculate the value.

public class Member


public string Name;

public DateTime DateOfBirth;

public bool IsAboveEighteen




bool result = (DateTime.Now – this.DateOfBirth).TotalDays > 365 * 18;

return result;




In the above example, the new property explores the existing property values to calculate its own value. The advantage is that there is no change of structure and no extra operations to achieve the desired result.


The following unit tests operate on the new property and display the result having IsAboveEighteen as true.


public void IsAboveEighteenTest()


IList<Member> list = new List<Member>()


new Member() { Name = “N1”, DateOfBirth= new DateTime(2000, 1, 1)},

new Member() { Name = “N2”, DateOfBirth= new DateTime(2000, 1, 1)},

new Member() { Name = “N3”, DateOfBirth= new DateTime(1990, 1, 1)},

new Member() { Name = “N4”, DateOfBirth= new DateTime(1980, 1, 1)}


var selectedList = list.Where(m => m.IsAboveEighteen);

foreach (Member member in selectedList)


Assert.AreEqual(2, selectedList.Count());


On running the test we can see the following output.


Extending Visitor in Sql Server

In Sql Server the same functionality can be achieve using Computed Columns. Even though Vistor Pattern is an Object Oriented Extension we can use the concept in database too.

Let us explore this with a simple example. We are having a table to store Transaction having Quantity and Price. The table is populated with data. We need to get the TotalPrice which is Quantity multiplied by Price. Without doing any data updating we can use Computed Column as shown below to achieve the results.



The result is shown below:


The above result is achieved without doing any data updating.


In this article we have explored the Visitor pattern. It allows us to be add more functionality without doing much change in the structure. Keeping this pattern in mind often helps in a better way of architecting.


6 Responses to “Visiting the Visitor Pattern”

  1. Rio said

    This explanation makes this pattern more understandable.

  2. Rio said

    If I want to add something like IsNameTooLong, would you have to make any change to class Member?
    If so, that’s against visitor pattern. If not, how do you do it?

    • Jean Paul said

      Hi Rio.. Nice to see your comment.

      About your query: I believe without making any change in the class we can achieve it.
      Using a bool property IsNameTooLong we can operate on the existing Name property, verify the length and return true/false.

      Jean Paul

      • Rio said

        As in your post, “Visitor lets you define a new operation without changing the classes of the elements”. No changing of the class is the key. By adding IsNameTooLong, you are making changes to that class. I’ll make a small example in my blog today and you can see if it make sense.

  3. Jean Paul said

    Hi Rio,

    I will try to clarify on your query.
    A class can contain properties of value types and other classes.
    For Eg: A Member class can contain a reference to Project class
    class Project
    bool IsActive
    .. other properties

    class Member
    int Id;
    string Name;
    IList Projects;

    To Get the number of projects active, you have to iterate through the Member.Projects property. Here we can define a new property of int and the getter of the property iterates through the elements in structure to get the operation done.

    class Member
    int Id;
    string Name;
    IList Projects;

    int ActiveProjectsCount
    get { return Projects.Where(p => p.IsActive).Count; }

    Here we achieved the need using Visitor pattern – without changing Project class or other properties of Member class.

    Hope now things are better 🙂

    Jean Paul

    • Rio said

      Yeah, that’s good. As longs as adding operations to class A doesn’t change anything in class A, it’s good. I wrote something in my today’s post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s