Printed from www.rmfusion.com A Developer website designed for Developers

Template Method Pattern Code Review

Code Download

Define the Pattern Interface and Handler Objects

Define the IPrimitive interface to be used as the "blueprint" for the main application classes.

interface IPrimitive { Int32 CompareTo(Object objectA, Object objectB); }

This interface defines the CompareTo() method. The CompareTo() method represents the generic method to compare two objects.

The PersonPrimitive class (object) defines the CompareTo() method to compare two objects of type Person.

class PersonPrimitive : IPrimitive { public Int32 CompareTo(Object objectA, Object objectB) { Person personA = objectA as Person; Person personB = objectB as Person; Int32 result;
if ((personA == null) || (personB == null)) { throw new InvalidCastException("This object is not of type Person"); } result = personA.Lastname.CompareTo(personB.Lastname);
if (result == 0) { result = personA.Firstname.CompareTo(personB.Firstname); } return (result); } }

The PersonPrimitive class represents the subclass that contains the actual sorting logic.

The CountryPrimitive class (object) defines the CompareTo() method to compare two objects of type String.

class CountryPrimitive : IPrimitive { public Int32 CompareTo(Object objectA, Object objectB) { Country countryA = objectA as Country; Country countryB = objectB as Country; Int32 result;
if ((countryA == null) || (countryB == null)) { throw new InvalidCastException("This object is not of type Country"); } result = countryA.CountryName.CompareTo(countryB.CountryName); return (result); } }

The CountryPrimitive class represents the subclass that contains the actual sorting logic.

The Algorithm class (object) represents the Template Method class containing the CompareTemplateMethod() method.

class Algorithm<T> { private IPrimitive m_primitive;
public Algorithm() { if (this is Algorithm<Person>) { this.m_primitive = new PersonPrimitive(); } if (this is Algorithm<Country>) { this.m_primitive = new CountryPrimitive(); } } public Int32 CompareTemplateMethod(Object objectA, Object objectB) { return(this.m_primitive.CompareTo(objectA, objectB)); } }

The CompareTemplateMethod() invokes the CompareTo() method in the relevant IPrimitive subclass.

Define the Country and Person Objects

The Person class (object) defines the structure for a person data object.

class Person : IComparable { public String Firstname { get; private set; } public String Lastname { get; private set; }
public Person(String firstName, String lastName) { this.Firstname = firstName; this.Lastname = lastName; } public int CompareTo(object obj) { Algorithm<Person> algoPerson = new Algorithm<Person>(); return(algoPerson.CompareTemplateMethod(this, obj)); } }

The Person class (object) contains the CompareTo() method, which will invoke the CompareTemplateMethod() method in the Template Method class.

The Country class (object) defines the structure for a country data object.

class Country : IComparable { public String CountryName { get; private set; }
public Country(String country) { this.CountryName = country; } public int CompareTo(object obj) { Algorithm<Country> algoCntry = new Algorithm<Country>(); return (algoCntry.CompareTemplateMethod(this, obj)); } }

The Country class (object) contains the CompareTo() method, which will invoke the CompareTemplateMethod() method in the Template Method class.

Use the Template Method Pattern

The Program class creates a list of Person object types and a list of Country object types. The Person and Country data is than sorted.

class Program { static void Main(string[] args) { List<Person> colPerson = new List<Person> { new Person("John", "Doe"), new Person("Jane", "Doe"), new Person("Alan", "Alda"), new Person("Joe", "Bloggs"), new Person("Anon", "Ymous") }; colPerson.Sort();
Console.WriteLine("*** Output of sorting Person objects ****\n"); foreach (Person person in colPerson) { Console.WriteLine(String.Format("{0}, {1}", person.Firstname, person.Lastname)); }
List<Country> colCntry = new List<Country> { new Country("United Kingdom"), new Country("United States"), new Country("Portugal"), new Country("France") }; colCntry.Sort();
Console.WriteLine("\n*** Output of sorting String objects ****\n"); foreach (Country cntry in colCntry) { Console.WriteLine(String.Format("{0}", cntry.CountryName)); }
Console.Read(); } }