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

Posts Tagged ‘design pattern visitor’

Visiting the Visitor Pattern

Posted by JP 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.

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.

Advertisements

Posted in C#, Design Patterns | Tagged: | 6 Comments »