Template Method Pattern

Description:

» The Template Method pattern enables algorithms to defer certain steps to subclasses.

Use When:

» Common behaviour can be factored out of an algorithm;
» The behaviour varies according to the type of a subclass.

//Define the Pattern Interface and Handler Objects.

//Define the IPrimitive interface to be used as the "blueprint" for the main application classes.
//This interface defines the CompareTo() method; the CompareTo() method represents the generic method to compare two objects.
public interface IPrimitive
{
int CompareTo(object objectA, object objectB);
}

//The PersonPrimitive class (object) defines the CompareTo() method to compare two objects of type Person.
//The PersonPrimitive class represents the subclass that contains the actual sorting logic.
public class PersonPrimitive : IPrimitive
{
public int CompareTo(object objectA, object objectB)
{
Person personA = (Person) objectA;
Person personB = (Person) objectB;
int result;

if (personA == null || personB == null)
{
throw new InvalidCastException("This object is not of type Person");
}

result = string.Compare(personA.Lastname, personB.Lastname, StringComparison.Ordinal);
if (result == 0)
{
result = string.Compare(personA.Firstname, personB.Firstname, StringComparison.Ordinal);
}

return result;
}
}

//The CountryPrimitive class (object) defines the CompareTo() method to compare two objects of type String.
//The CountryPrimitive class represents the subclass that contains the actual sorting logic.
public class CountryPrimitive : IPrimitive
{
public int CompareTo(object objectA, object objectB)
{
Country countryA = (Country) objectA;
Country countryB = (Country) objectB;

if (countryA == null || countryB == null)
{
throw new InvalidCastException("This object is not of type Country");
}

int result = string.Compare(countryA.CountryName, countryB.CountryName, StringComparison.Ordinal);

return result;
}
}

//The Algorithm class (object) represents the Template Method class containing the CompareTemplateMethod() method.
//The CompareTemplateMethod() invokes the CompareTo() method in the relevant IPrimitive subclass.
public class Algorithm<T>
{
private IPrimitive m_primitive;

public Algorithm()
{
if (GetType() == typeof(Algorithm<Person>))
{
m_primitive = new PersonPrimitive();
}

if (GetType() == typeof(Algorithm<Country>))
{
m_primitive = new CountryPrimitive();
}
}

public int CompareTemplateMethod(object objectA, object objectB)
{
return m_primitive.CompareTo(objectA, objectB);
}
}
Image
//The Algorithm class (object) represents the Template Method class containing the CompareTemplateMethod() method.
//The CompareTemplateMethod() invokes the CompareTo() method in the relevant IPrimitive subclass.
public class Algorithm<T>
{
private IPrimitive m_primitive;

public Algorithm()
{
if (GetType() == typeof(Algorithm<Person>))
{
m_primitive = new PersonPrimitive();
}

if (GetType() == typeof(Algorithm<Country>))
{
m_primitive = new CountryPrimitive();
}
}

public int CompareTemplateMethod(object objectA, object objectB)
{
return m_primitive.CompareTo(objectA, objectB);
}
}