Click here to Skip to main content
Full site     10M members (32.5K online)    

Template Pattern Concept with Example

Introduction

In software engineering, the template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in a method, called template method, which defers some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm's structure.

By Wiki

In template pattern, we have:

As the inheritance definition inherited class, it may have its own algorithm functionality having base algorithm as template method.

Background

Let me start my story.

We have Tea without Sugar template containing template method that is found by Jone and Kedar.

Both use that template to make tea in their own shop.

Jone is making tea without sugar in his tea shop by re defining certain steps of Tea template.

Kedar is making tea without sugar in his tea shop by redefining certain steps of Tea template, moreover he is making tea with sugar by having its own functionality and using base functionality of tea without sugar.

Let’s find out the above mentioned scenario in C# code.

Using the Code

I am defining an abstract class containing the template method.

public abstract class TeaTemplate
{
    public abstract void AddMilk();
    public abstract void AddTeaPowder();
    public abstract void BoileMilk();
    public void MakeTeaWithoutSugar()
    {
        AddMilk();
        AddTeaPowder();
        BoileMilk();
    }
} 

Jone implements the template class and makes tea without sugar.

public class JoneTeaShop : TeaTemplate
{
    public override void AddMilk()
    {
        Console.WriteLine("Add ABC PVT LTD Milk.");
    }
    public override void AddTeaPowder()
    {
        Console.WriteLine("Add PQR PVT LTD Tea Leaf Powder.");
    }
    public override void BoileMilk()
    {
        Console.WriteLine("Boil Milk using coal.");
    }
} 

Kedar implements the template class and makes tea without sugar. Moreover, he is making tea with sugar by having its own functionality and using base functionality of tea without sugar.

public class KedarTeaShop : TeaTemplate
{
    public override void AddMilk()
    {
        Console.WriteLine("Add COWBEST LTD Milk.");
    }
    public override void AddTeaPowder()
    {
        Console.WriteLine("Add ASAM Tea Leaf Powder.");
    }
    public override void BoileMilk()
    {
        Console.WriteLine("Boil Milk using Burner.");
    }
    public void MakeTeaWithSugar()
    {
        MakeTeaWithoutSugar();
        Console.WriteLine("Add SUGARCANE LTD Sugar");
    }
} 

Let's create an object of Jone and Kedar to find out what kind of tea they make.

 static void Main(string[] args)
{
    Console.WriteLine("Jone Shop....");
    JoneTeaShop joneTeaShop = new JoneTeaShop();
    joneTeaShop.MakeTeaWithoutSugar(); //Jone shop making tea without sugar 
    		//using Tea template default algoritham with its own implementation.
    
    Console.WriteLine("\nKedar Shop....");
    KedarTeaShop kedarTeaShop = new KedarTeaShop();
    Console.WriteLine("Menu 1:-");
    kedarTeaShop.MakeTeaWithoutSugar(); //Kedar making tea without sugar 
    		//using Tea template default algoritham with its own implementation.
    Console.WriteLine("Menu 2:-");
    kedarTeaShop.MakeTeaWithSugar(); //Kedar added more stemp to algoritham where 
    	//original algoritham stemps remains unchanged having his own implementation.
    Console.Read();
} 

So we have TeaTemplate class having method MakeTeaWithoutSugar(), called template method, which defers some steps to subclasses JoneTeaShop and KedarTeaShop. It lets one redefine certain steps of an algorithm without changing the algorithm's structure.

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search 
Per page   
GeneralMy vote of 5
Mazen el Senih
11 May '13 - 6:29 
SuggestionTemplate method should not be overrridable.
6220119
28 Apr '13 - 16:50 
QuestionA couple of questions.
George Swan
27 Apr '13 - 22:32 

Last Updated 27 Apr 2013 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2013