Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The Template Method Pattern

Posted in Design Patterns
This post has been read 2934 times

In continuation with the series on design pattern, I am currently going through Behavioral Patterns. Today I will write about the Template Method Pattern. You can read about rest of the patterns from the following links

  1. Strategy Pattern
  2. State Pattern

You can read about the Structural patterns from here.
You can read about the Creational patterns from here.

The template method pattern deals with enabling algorithms to defer certain steps. It doesn't change the structure of the algorithm but a few of their operations are handled elsewhere.

    1 namespace PatternsConsole

    2 {

    3     class TemplatePattern

    4     {

    5         //Interface for operations

    6         interface ISortStrategy

    7         {

    8             void Sort();

    9         }

   10 

   11         //Algorithm with template method

   12         class Algorithm

   13         {

   14             public void TemplateMethod(ISortStrategy sortStrategy)

   15             {

   16                 sortStrategy.Sort();

   17             }

   18         }

   19 

   20         //Concrete Class

   21         class SortByAge : ISortStrategy

   22         {

   23             public void Sort()

   24             {

   25                 PersonList.Sort( ( p1, p2 ) => p1.Age.CompareTo( p2.Age ) );

   26                 Console.WriteLine("Sort By Age");

   27                 foreach ( var person in PersonList )

   28                 {

   29                     Console.WriteLine( "Name : " + person.Name + " Age :" + person.Age);

   30                 }

   31             }

   32         }

   33 

   34         //Concrete Class

   35         class SortByName : ISortStrategy

   36         {

   37             public void Sort()

   38             {

   39                 PersonList.Sort( ( p1, p2 ) => p1.Name.CompareTo( p2.Name ) );

   40                 Console.WriteLine( "\nSort By Name" );

   41                 foreach ( var person in PersonList )

   42                 {

   43                     Console.WriteLine( "Name : " + person.Name + " Age :" + person.Age );

   44                 }

   45             }

   46         }

   47 

   48         class Person

   49         {

   50             public string Name { get; set; }

   51             public int Age { get; set; }

   52         }

   53 

   54         private static List<Person> PersonList;

   55 

   56         private static void Main()

   57         {

   58             PersonList = new List<Person>

   59                               {

   60                                   new Person {Name = "Person1", Age = 25},

   61                                   new Person {Name = "Person3", Age = 26},

   62                                   new Person {Name = "Person4", Age = 20},

   63                                   new Person {Name = "Person2", Age = 28}

   64                               };

   65 

   66             var sortAlgo = new Algorithm();

   67 

   68             sortAlgo.TemplateMethod( new SortByAge() );

   69             //Output :  Sort By Age

   70             //          Name : Person4 Age :20

   71             //          Name : Person1 Age :25

   72             //          Name : Person3 Age :26

   73             //          Name : Person2 Age :28

   74 

   75             sortAlgo.TemplateMethod( new SortByName() );

   76             //Output :  Sort By Name

   77             //          Name : Person1 Age :25

   78             //          Name : Person2 Age :28

   79             //          Name : Person3 Age :26

   80             //          Name : Person4 Age :20

   81 

   82             Console.Read();

   83         }

   84     }

   85 }

We can use the template method pattern when common behavior can be outside the algorithm and the the behavior itself varies based on the type of the subclass.

In my next post I will be writing about the Chain of Responsibility pattern.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll