Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Template Pattern Concept with Example

0.00/5 (No votes)
27 Apr 2013 1  
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.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here