Sunday, December 16, 2018

Template Method Pattern

Template Method design pattern to define an algorithm as skeleton of operations and leave the details to be implemented by child classes. Template Method Pattern falls under behavioral pattern of GOF (Gang of Four) patterns.

When to use –


Template Method pattern used to define basic steps or structure of an algorithm and allow the implementation of each steps in derived class. Template method pattern has two components. One is an abstract class and one or more concrete classes. An abstract class is the Template class, which defines algorithmic steps and structure and preserves it across multiple implementations.  

Major components of Template Method pattern –


Abstract Class – This is an abstract class contains template method and abstract operations.
Concrete Class – This class inherits Abstract class and overrides abstract methods.

Let’s have a look on below example of Template Method design pattern.

Code –
//Template abstract class
public abstract class OrderTemplate
{
    public abstract void SelectProduct();
    public abstract void Payment();
    public abstract void Deliver();
    public void ProcessOrder()
    {
        SelectProduct();
        Payment();
        Deliver();
    }
}
//concrete classe
public class OnLineOrder : OrderTemplate
{
    public override void Deliver()
    {
        Console.WriteLine("Product shipped via courier");
    }

    public override void Payment()
    {
        Console.WriteLine("Online payment done successfully");
    }

    public override void SelectProduct()
    {
        Console.WriteLine("Product added to cart successfully");
    }
}
//concrete class
public class StoreOrder : OrderTemplate
{
    public override void Deliver()
    {
        Console.WriteLine("Product delivered to customer at counter");
    }

    public override void Payment()
    {
        Console.WriteLine("Paid at counter");
    }

    public override void SelectProduct()
    {
        Console.WriteLine("Product is selected by customer from rack in store");
    }
}
class Program
{
    //entry point
    static void Main(string[] args)
    {
        Console.WriteLine("----------- Online Order Process -----------");
        OrderTemplate onlineOrder = new OnLineOrder();
        onlineOrder.ProcessOrder();
        Console.WriteLine("----------- Retail Store Order Process -----------");
        OrderTemplate storeOrder = new StoreOrder();
        storeOrder.ProcessOrder();
        Console.Read();
    }
}

Output –



As per above example, OrderTemplate abstract class contains SelectPrduct, Payment and Deliver methods. ProcessOrder method is Tempate method that calls these methods in sequence. OnLineOrder and StoreOrder classes derived from OrderTemplate abstract class and overrides abstracts methods differently in each class.

You can download full code from Gist.

I hope this article helps you to know more about Template Method Design Pattern. Please leave your feedback in comments section below.

References –

See Also –

No comments:

Post a Comment