Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#
Tip/Trick

Template Pattern Concept with Example

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
27 Apr 2013CPOL2 min read 18.5K   44   3   6
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:

  • Abstract class called as the template class act as skeleton to all inherited classes.
  • Abstract class defines generic step of algorithm.
  • Abstract class contains template method that defines the sequence of steps of algorithm called template method.
  • Inherited class allows redefining certain steps of algorithm without changing algorithm step sequence.

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.

Image 1

Using the Code

I am defining an abstract class containing the template method.

C#
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.

C#
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.

C#
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.

C#
 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Lionbridge
India India
Amey K Bhatkar, a “Microsoft .Net” Web Developer.
I am programmer by will and profession.
I have completed my MCA in 2011 and join software industry.
Presently I am working with Lion Bridge Technologies in Mumbai - India

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manikandan1030-Jun-14 5:05
professionalManikandan1030-Jun-14 5:05 
GeneralMy vote of 5 Pin
Mazen el Senih11-May-13 6:29
professionalMazen el Senih11-May-13 6:29 
SuggestionTemplate method should not be overrridable. Pin
622011928-Apr-13 16:50
622011928-Apr-13 16:50 
GeneralRe: Template method should not be overrridable. Pin
Amey K Bhatkar24-May-13 0:33
Amey K Bhatkar24-May-13 0:33 
QuestionA couple of questions. Pin
George Swan27-Apr-13 22:32
mveGeorge Swan27-Apr-13 22:32 
AnswerRe: A couple of questions. Pin
Amey K Bhatkar24-May-13 0:44
Amey K Bhatkar24-May-13 0:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.