Template Method Design Pattern Real-time Example

Template Method Design Pattern Real-time Example

In this article, I am going to discuss the Template Method Design Pattern Real-time Example in C#. Please read our previous article where we discussed the Template Method Design Pattern in C#.

Template Method Design Pattern Real-time Example: Coffee

Suppose you want to prepare Coffee (let say BruCoffee). Then you need to follow some steps or procedures such as Boil Water, Add Milk, Add Sugar, and Add BruCoffee which is shown in the following image.

Template Method Design Pattern Real-time Example

Suppose you want to prepare Nescafe Coffee. Then also you need to follow the same procedure as BruCofee such as Boil Water, Add Milk, Add Sugar, and Add Nescafe Coffee as shown in the following image.

Template Method Design Pattern Real-time Example

If you see both the coffee preparation procedure, it is almost the same i.e. Boil Water, Add Milk, Add Sugar, Add Coffee type (i.e. Bru and Nescafe), and based on the Coffee it will create either Bru Coffee or Nescafe Coffee. So, the procedure for creating coffee is the same. What I can do here is, I can create some template method (PrepareCoffee) and within that PrepareCoffee template method, I can define the procedure to create the coffee. For better understanding please have a look at the following diagram.

Real-Time Example of Template Method Design Pattern in C#

Template Method Design Pattern Real-Time Example Implementation – Coffee

Let implement the above example step by step using C#.

Step1: Creating the Template Method

Create a class file with the name CoffeeTemplate.cs and then copy and paste the following code in it. This is going to be an abstract class that defines a set of abstract methods that need to be implemented by the concrete subclasses. But if you want then you can also provide a default implementation and the child class can optionally override the implementation.

This abstract class also provides one concrete method (i.e. template method) which defines the order in which those abstract operations should occur.

using System;
namespace TemplateDesignPattern
{
    public abstract class CoffeeTemplate
    {
        // PrepareCoffee method is the template method
        public void PrepareCoffee()
        {
            BoilWater();
            AddMilk();
            AddSugar();
            AddCoffeePowder();
            Console.WriteLine(this.GetType().Name + " is Ready");
        }

        protected abstract void BoilWater();
        protected abstract void AddMilk();
        protected abstract void AddSugar();
        protected abstract void AddCoffeePowder();
    }
}
Step2: Creating Concrete Class.

The Concrete Classes are going to implement the operations defined by the Abstract Class. As we are going to create two types of coffees i.e. BruCoffee and NescafeCoffee so we are going to create two concrete classes by implementing the abstract CoffeeTemplate class.

BruCoffee:

Create a class file with the name BruCoffee.cs and then copy and paste the following code in it.

using System;
namespace TemplateDesignPattern
{
    public class BruCoffee : CoffeeTemplate
    {
        protected override void BoilWater()
        {
            Console.WriteLine("Water Boild");
        }

        protected override void AddMilk()
        {
            Console.WriteLine("Milk Added");
        }

        protected override void AddSugar()
        {
            Console.WriteLine("Sugar Added");
        }
        protected override void AddCoffeePowder()
        {
            Console.WriteLine("Bru Coffee Powder Added");
        }
    }
}
NescafeCoffee:

Create a class file with the name NescafeCoffee.cs and then copy and paste the following code in it.

using System;
namespace TemplateDesignPattern
{
    public class NescafeCoffee : CoffeeTemplate
    {
        protected override void BoilWater()
        {
            Console.WriteLine("Water Boild");
        }

        protected override void AddMilk()
        {
            Console.WriteLine("Milk Added");
        }

        protected override void AddSugar()
        {
            Console.WriteLine("Sugar Added");
        }
        protected override void AddCoffeePowder()
        {
            Console.WriteLine("Nescafe Coffee Powder Added");
        }
    }
}
Step3: Client

Modify the Main method as shown below. Here, we are creating an object of coffee type and then calling the PrepareCoffee method. Based on the type it will create appropriate coffee.

using System;
namespace TemplateDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Nescafe Coffee Preparation\n");
            CoffeeTemplate coffee = new NescafeCoffee();
            coffee.PrepareCoffee();

            Console.WriteLine();

            Console.WriteLine("Bru coffee preparation\n");
            coffee = new BruCoffee();
            coffee.PrepareCoffee();

            Console.Read();
        }
    }
}

Output:

Template Method Design Pattern in C# with Real-Time Examples

In the next article, I am going to discuss the Command Design Pattern in C# with real-time examples. Here, in this article, I try to explain the Template Method Design Pattern Real-Time Example in C#. I hope you enjoy this Template Method Design Pattern Real-Time Example in C# article.

2 thoughts on “Template Method Design Pattern Real-time Example”

  1. blank

    Brilliantly explained that now I can figure out the problem in my project and improve it using this design pattern.

  2. blank

    coffee.PrepareCoffee doesn’t make sense in the real world.
    NescafeTemplate or NescafeMakerTemplate sounds better to me.
    e.x)
    nescafeTemplate.MakeProduct() : Nescafe
    The single responsibility of the template is now a CoffeeMaker, so we need to write new classes derived from a superclass Coffee.

Leave a Reply

Your email address will not be published. Required fields are marked *