Sunday 15 April 2012

Visitor Pattern : Revisited

Some developers find design patterns an "Advance" topic. But the reality is that we use design pattern every day and mostly without even knowing.

I find design patterns as an essential element in everyday development. I agree that reading the "Design Patterns" by the Gang of Four (GOF) can be challenging. But trust me, its worth the effort. Although I have not read this book cover to cover, it is an essential reference that you should have access to.

The intent of Visitor pattern according to GOF is "Represent an operation to be performed on elements of an object structure. Visitor lets you to define a new operation without changing the classes of the elements on which it operates.". I think for a developer, the second sentence is the most important piece "without changing the classes".

Consider an example where two tax rate applied to people based on their earnings. The idea here is to  create a "Visitor" to calculate the tax.

[Lets have a quick look at the UML. - Created using Creately]

















Lets start with the "Person" class. This is an abstract class.


    /// <summary>
    /// This is the base class for people.
    /// </summary>
    public abstract class Person
    {
        public string Name { getprivate set; }
 
        protected Person(string name)
        {
            Name = name;
        }
 
        /// <summary>
        /// Accept a visitor so that new functionality can be added to the <see cref="Person"/>.
        /// </summary>
        public abstract void Accept(IVisitor visitor);
    }


The above immutable class has a method called "Accept". This is where we "inject" the visitor. The "Person" class is ready to entertain a "Visitor".

The next step is to create the two types of people. In this example they are high and low earners.


    /// <summary>
    /// This is a concrete class of a <see cref="Person"/>.
    /// </summary>
    public class HighEarningPerson : Person
    {
        public decimal Salary { getprivate set; }
 
        public decimal Rate { getprivate set; }
 
        public HighEarningPerson(string name, decimal salary, decimal rate)
            : base(name)
        {
            Salary = salary;
            Rate = rate;
        }
 
        /// <summary>
        /// The Visitor to this object works on the <see cref="HighEarningPerson"/> instance.
        /// </summary>
        public override void Accept(IVisitor visitor)
        {
            visitor.Visit(this);
        }
    }


This is followed by the low earning person.


/// <summary>
    /// This is a concrete class of a <see cref="Person"/>.
    /// </summary>
    public class LowEarningPerson : Person
    {
        public decimal Salary { getprivate set; }
 
        public decimal Rate { getprivate set; }
 
        public LowEarningPerson(string name, decimal salary, decimal rate)
            : base(name)
        {
            Salary = salary;
            Rate = rate;
        }
 
        /// <summary>
        /// The Visitor works on the instance of <see cref="LowEarningPerson"/>.
        /// </summary>
        /// <param name="visitor"></param>
        public override void Accept(IVisitor visitor)
        {
            visitor.Visit(this);
        }
    }


Both classes looks similar, but take a closer look at the "Accept" method. The "Accept" method receives the "Visitor" and calls the "Visit" method passing in the current object. The "Visitor" is now aware of the type of person it is dealing with.

The following is the interface of the "IVisitor".


    /// <summary>
    /// This is the interface of the Visitor that 
    /// works on the concrete types of people. 
    /// </summary>
    public interface IVisitor
    {
        void Visit(HighEarningPerson highEarningPerson);
 
        void Visit(LowEarningPerson lowEarningPerson);
    }


The "IVisitor" works with the concrete "Person" types. So in this case "HighEarningPerson" and "LowEarningPerson". You can see that "IVisitor" is using the method overloading.

Lets look at the implementation of the "IVisitor".


    /// <summary>
    /// This is the Visitor that calculates the tax for each person.
    /// </summary>
    public class TaxVisitor : IVisitor
    {
        private readonly ITaxCalculatorFactory _taxCalculatorFactory;
 
        public TaxVisitor(ITaxCalculatorFactory taxCalculatorFactory)
        {
            _taxCalculatorFactory = taxCalculatorFactory;
        }
 
        public void Visit(HighEarningPerson highEarningPerson)
        {
            var taxCalculator = _taxCalculatorFactory.Create(TaxType.High);
            var tax = taxCalculator.Calculate(highEarningPerson.Salary, highEarningPerson.Rate);
 
            Console.WriteLine("The tax for '{0} - HIGH' is £{1:00}", highEarningPerson.Name, tax);
        }
 
        public void Visit(LowEarningPerson lowEarningPerson)
        {
            var taxCalculator = _taxCalculatorFactory.Create(TaxType.Low);
            var tax = taxCalculator.Calculate(lowEarningPerson.Salary, lowEarningPerson.Rate);
 
            Console.WriteLine("The tax for '{0} - LOW' is £{1:00}", lowEarningPerson.Name, tax);
        }
    }


Do not pay too much attention to the "ITaxCalculatorFactory" that is used here. The reason for using the "ITaxCalculatorFactory" is to keep the creation of the objects out from business processes. Always keep in a note of the Inversion of Control (IoC).

The "TaxVisitor" calculates the tax rate of each person and displays the result.

Lets see how all of this fits in..

I have created a "Console" application. See the following class.


public class VisitorPattern : ITest
    {
        public void Execute()
        {
            Console.WriteLine("**** VISITOR Pattern *****");
            var lowTaxRate = 0.023M;
            var highTaxRate = 0.04M;
 
            var people = new List<Person>()
                             {
                                 new LowEarningPerson("Bob", 23000, lowTaxRate),
                                 new HighEarningPerson("Alice", 45000, highTaxRate),
                                 new LowEarningPerson("Peter", 13000, lowTaxRate),
                                 new HighEarningPerson("Pan", 100000, highTaxRate)
                             };
 
            var taxCalculatorFactory = new TaxCalculatorFactory();
            var taxVisitor = new TaxVisitor(taxCalculatorFactory);
 
            people.ForEach(person => person.Accept(taxVisitor));
        }
    }


We have a collection of "Person" objects. Each has its own type (Low and High earnings). The "TaxVisitor" visits each object and calculates the tax. The output looks like below.


We can clearly see that correct calculate method in the "TaxVisitor" is been called based on the type of the object.

The Visitor pattern is uses the "double-dispatch" to resolve a method call. Double-dispatch simply means the ability to choose the correct method at run-time. So in the above case, although we iterate over a collection of "Person" objects, the run-time type has been used to pick the correct method in the visitor.

Hope you will find this note useful.

Sunday 8 April 2012

When it comes to bugs...

I have been doing more documentation (than an average developer) these days. I do understand how frustrating it can be to share a technical concept with a non-technical person.

I would like to share the following link related to the recent disruption to Microsoft Azure cloud computing platform. I have read many bug reports from Microsoft during the years and following in my book is ranked the highest. 


This report is clear, concise and transparent (which is not very common when it comes to software bugs.)

Thursday 5 April 2012

Another look at Extension Methods


The C# 3 has somewhat revolutionised the way we use to write anonymous method. I am almost ready to say that lambda expression has rendered anonymous method “obsolete”. I agree that there are restrictions in anonymous methods such as it should be inline and cannot have multiple statements. 

The scope of this post is not to discuss about anonymous methods, but to look at much neater way of writing static utility methods. Please welcome Extension methods!

The development community have a wide range of ideas about extension methods, and some goes all the way of discounting it from the development toolset. I was reading the chapter on extension methods in Jon Skeet’s C# In Depth 2ed Edition (which I have to say probably one of the best investments I have done), and would like to share an example that I saw in this book.  See below.

C# In-Depth 2ed Edition – Jon Skeet (Chapter 10)



    public class Test
    {
        static void Main()
        {
            object y = null;

            Console.WriteLine(y.IsNull());

            y = new object();
            Console.WriteLine(y.IsNull());
        }
    }


    public static class NullUtil
    {
        public static bool IsNull(this object x)
        {
            return x == null;
        }
    }




Looking at the above code for the first time gave be the frights! For the C# gurus out there, it might be a piece of cake!

To the naked eye this code looks crazy, but for the person who knows the key concepts of extension methods, this is a simple exercise. 

Consider the following piece of code (from LinqPad).  

void Main()
{
       IEnumerable<int> myEnumerable = null;      
       var fives = myEnumerable
              .EmptyIfNull()
              .Where(x => x == 5)
              .ToList();
             
       fives.Dump();
}

// Define other methods and classes here
public static class EnumerableEx
{
       public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> enumerable)
       {
              return enumerable ?? Enumerable.Empty<T>();
       }
}


Please do not tell me that the above code is not sweet! I agree that this depends on the business case, but consider the possibilities. How often do you use FirstOrDefault() and check whether the return value is null.

 I use to be sceptical about extension methods, but now I am a believer!