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();
Console.WriteLine("\nKedar Shop....");
KedarTeaShop kedarTeaShop = new KedarTeaShop();
Console.WriteLine("Menu 1:-");
kedarTeaShop.MakeTeaWithoutSugar(); Console.WriteLine("Menu 2:-");
kedarTeaShop.MakeTeaWithSugar(); 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.