Visiting the Visitor Pattern

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

Challenge

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.

Definition

“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.”

Implementation

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

{

get

{

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.

clip_image002

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

[TestMethod()]

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)

Console.WriteLine(member.Name);

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

}

On running the test we can see the following output.

clip_image004

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.

clip_image006

clip_image008

The result is shown below:

clip_image010

The above result is achieved without doing any data updating.

Summary

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 thoughts on “Visiting the Visitor Pattern

  1. Rio says:

    This explanation makes this pattern more understandable.

  2. Rio says:

    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?

    1. Jean Paul says:

      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.

      Regards
      Jean Paul

      1. Rio says:

        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 says:

    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 :)

    Regards,
    Jean Paul

    1. Rio says:

      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 to Jean Paul Cancel reply

Your email address will not be published. Required fields are marked *