Template Method Design Pattern in C#

Template Method Design Pattern in C#

In this article, I am going to discuss the Template Method Design Pattern in C# with examples. Please read our previous article where we discussed State Design Pattern in C# with real-time examples. The Template Method Design Pattern falls under the category of behavioral Design Pattern. As part of this article, we are going to discuss the following pointers.

  1. What is the Template Method Design Pattern?
  2. Understanding the Template Method Design Pattern with Real-Time example
  3. Implementation of the Template Method Design Pattern in C#.
  4. When to use the Template Method Design Pattern in real-time applications?
What is the Template Method Design Pattern?

The Template Method Design Pattern defines a sequence of steps of an algorithm and allows the subclasses to override the steps but not allowed to change the sequence.

The Key to the Template Design Pattern is that we put the general logic in the abstract parent class and let the child classes define the specifics.

Let us understand the above explanation with an example. Suppose you want to build a concrete house, then you need to follow a sequence of steps such as first you need to build a foundation, then you need to build Pillars. Third, you need to build Walls and finally, you need to build Windows as shown in the below image.

Understanding the Template Method Design Pattern in C#

So, you have to follow the above sequence of procedures in order to build a concrete house. Suppose you want to build a wooden house. Then also you need to follow the same sequence of procedures that you follow in order to build a concrete house i.e. first you need to build the foundation, then you need to build Pillars, then Walls and Finally Windows as shown in the below image.

Understanding the Template Design Pattern in C#

The only difference here is instead of using Concrete you need to use Wooden Materials.

So, what you can do here is, you can define a template method (called Build a House). The template method will define what are all the procedures or steps (i.e. step1: Building Foundation, Step2: Building Pillars, Step3: Building Walls, Step4: Building Windows) that you need to follow in order to build a house. So, for both the house (Concrete and Wooden) you need to above steps which are shown in the below image.

Template Method Design Pattern in C#

So, using the Template method you can build any type of house (Wooden, Glass, Concrete, etc).

Implementation of Template Method Design Pattern in C#:

Let us implement the above example (Building House) using Template Method Design Pattern in C# step by step.

Step1: Creating the Template Method in Abstract class

Create an abstract class with the name HouseTemplate and then copy and paste the following code in it. This Abstract Class defines a set of abstract methods that need to be implemented by the Concrete sub Classes. But if you want you can also provide a default implementation for the above methods and the child class can optionally override the implementation. It also provides one concrete method (i.e. template method) which defines the order in which those abstract operations occur. 

using System;
namespace TemplateMethodDesignPattern
{
    public abstract class HouseTemplate
    {
        // Template method defines the sequence for building a house
        public void BuildHouse()
        {
            BuildFoundation();
            BuildPillars();
            BuildWalls();
            BuildWindows();
            Console.WriteLine("House is built");
        }

        // Methods to be implemented by subclasses
        protected abstract void BuildFoundation();
        protected abstract void BuildPillars();
        protected abstract void BuildWalls();
        protected abstract void BuildWindows();
    }
}
Step2: Creating Concrete Class.

The Concrete Class implements the operations defined by the Abstract Class. As we are going to create two types of house i.e. Concrete and Wooden so we are going to create two concrete classes by implementing the abstract HouseTemplate class.

ConcreteHouse:

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

using System;
namespace TemplateMethodDesignPattern
{
    public class ConcreteHouse : HouseTemplate
    {
        protected override void BuildFoundation()
        {
            Console.WriteLine("Building foundation with cement, iron rods and sand");
        }

        protected override void BuildPillars()
        {
            Console.WriteLine("Building Concrete Pillars with Cement and Sand");
        }

        protected override void BuildWalls()
        {
            Console.WriteLine("Building Concrete Walls");
        }

        protected override void BuildWindows()
        {
            Console.WriteLine("Building Concrete Windows");
        }
    }
}
WoodenHouse:

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

using System;
namespace TemplateMethodDesignPattern
{
    public class WoodenHouse : HouseTemplate
    {
        protected override void BuildFoundation()
        {
            Console.WriteLine("Building foundation with cement, iron rods, wood and sand");
        }

        protected override void BuildPillars()
        {
            Console.WriteLine("Building wood Pillars with wood coating");
        }

        protected override void BuildWalls()
        {
            Console.WriteLine("Building Wood Walls");
        }

        protected override void BuildWindows()
        {
            Console.WriteLine("Building Wood Windows");
        }
    }
}
Step3: Client

Modify the Main method as shown below. Here, we are creating an object of house type and calling the BuildHouse method.

using System;
namespace TemplateMethodDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Build a Concrete House\n");
            HouseTemplate houseTemplate = new ConcreteHouse();
            // call the template method
            houseTemplate.BuildHouse();

            Console.WriteLine();

            Console.WriteLine("Build a Wooden House\n");
            houseTemplate = new WoodenHouse();
            // call the template method
            houseTemplate.BuildHouse();

            Console.Read();
        }
    }
}

Output:

Real-Time Example of Template Method Design Pattern

In the next article, I am going to discuss some more real-time examples using the Template Method Design Pattern in C#. Here, in this article, I try to explain the Template Method Design Pattern in C# with an example. I hope you enjoy this article.

Leave a Reply

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