Click here to Skip to main content
Click here to Skip to main content

Template Pattern Concept with Example

, 27 Apr 2013
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Amey K Bhatkar
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
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalManikandan1030-Jun-14 5:05 
GeneralMy vote of 5 PinmemberMazen el Senih11-May-13 6:29 
SuggestionTemplate method should not be overrridable. Pinmember622011928-Apr-13 16:50 
GeneralRe: Template method should not be overrridable. [modified] PinprofessionalAmey K Bhatkar24-May-13 0:33 
QuestionA couple of questions. PinmemberGeorge Swan27-Apr-13 22:32 

Thanks for the interesting example. I thought that it was the class method that’s considered to be templated rather than the class itself. So it’s a TeaMaking class containing a template method, namely, MakeTeaWithoutSugar. Is this right? Also, wouldn’t it be better to make the public abstract methods protected to ensure that subclasses are only derived from the abstract class and not another subclass? Best wishes.


AnswerRe: A couple of questions. PinprofessionalAmey K Bhatkar24-May-13 0:44 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 27 Apr 2013
Article Copyright 2013 by Amey K Bhatkar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid