SIGN UP MEMBER LOGIN:    
ARTICLE

Visiting the Visitor Pattern

Posted by Jean Paul Articles | .NET 4.5 November 19, 2011
Visitor Pattern is one among the 23 design patterns. It is less known but holds a good idea for OOPs enthusiasts.
Reader Level:
Download Files:
 

Challenge
 
You have 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 Members who have an age greater than 18.
 
One way is to 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 Date-Of-Birth 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.

Visitor Pattern
 
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.

Visitor Pattern in .NET
 
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.

Pattern
 
The result is shown below:

Visitor Pattern sql
 
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.
  

Login to add your contents and source code to this article
share this article :
post comment
 

Thank You Vikas once again for the encouragements. I am working on other design patterns. I need your feedback there too.

Posted by Jean Paul Nov 26, 2011

k........its a great work..........

Posted by Vikas Mishra Nov 26, 2011

Thank You Vikas for the review and comments. It is great to see your enthusiasm towards patterns. It is true that some of the books we refer have complicated examples which takes away the enjoyment of learning the pattern. I am working on more patterns with an easier understanding possibility. Regards, Jean Paul

Posted by Jean Paul Nov 21, 2011

Hi sir, its really a very interesting article and you describe it in a very interesting way,and also in a very easy technique . thanks...........

Posted by Vikas Mishra Nov 21, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor