Explanation of Template Design Pattern

Explanation of Template Design Pattern

Hello everyone, in this article we are going to talk about Template design pattern within Behavioral Design Patterns. We will talk what it is and then we are going to make an example with C# and Visual Studio.

Let's begin.

The Template Design Pattern Defines the general operation of am algorithm or a task but it leaves the steps operations to sub classes. The exact operations are defined at sub classes. So we do not change the main operation, we change the sub classes according to any algorithm changes or we already defined it for different type of operations.

In this design pattern we have two main parts: AbstractClass and ConcreteClass.

  • AbstractClass defines the algorithm and what operations do we need for ConcreteClass. In here we define the template of the operations.
  • ConcreteClass implements the the step operations of different type of algorithms according to Abstract Class template.

Before we start the example you should take a look at below article. I used somethings within this article : Type Class and typeof Keyword and GetType in C#

In our example we are going to make different types of soups. Almosts every soups have same mixtures : Tomatoes, paste, onions and water. The other elements are changing according to kind of soups.

First I have to create my abstract class. In this class I will have my abstract and virtual methods to get a soup ready. After I will derive soup classes from this abstract class.

Below image you can see the schematic of my example application. Template Design Pattern Schematic

First I need to define my abstract class for my operation. In here I have two essential methods and I defined is as abstract. Also There is a virtual methods for optional operations, I declared as virtual method. The derived class does not havve to use it.
And last I have a method which perform all required tasks.

Soup.cs

using System;

namespace TemplatePattern_Example
{
    public abstract class Soup
    {
        //My abstract methods
        public abstract void AddMaterials();
        public abstract void CookSoup();

        //My virtual method
        public virtual void AddSpecialMaterial(string specialMaterial)
        {
            Console.WriteLine("The Special material " + specialMaterial + " for " + GetType().Name + " has been added");
        }

        //My get Ready method.
        public void GetSoupReady()
        {
            AddMaterials();
            CookSoup();

            Console.WriteLine(GetType().Name + " is ready... Enjoooy :)");
            Console.WriteLine("-----------------------------" + Environment.NewLine);
        }
    }
}

Now I need to derive the sub classes which perform the specific operations. Below code blocks you will see my sub classes.

TomatoSoup.cs

using System;

namespace TemplatePattern_Example
{
    class TomatoSoup : Soup
    {
        public override void AddMaterials()
        {
            Console.WriteLine("Adding Essential Materials for Tomato Soup...");
        }

        public override void CookSoup()
        {
            Console.WriteLine("Tomato Soup is cooking...");
        }
    }
}

ChickenSoup.cs

using System;

namespace TemplatePattern_Example
{
    class ChickenSoup : Soup
    {
        public override void AddMaterials()
        {
            Console.WriteLine("Adding Essential Materials for Chicken Soup...");
            //I also add some other materials except the main materials
            AddSpecialMaterial("Delicious Chicken meat");
        }

        public override void CookSoup()
        {
            Console.WriteLine("Chicken Soup is cooking...");
        }
    }
}

EzoGelinSoup.cs

using System;

namespace TemplatePattern_Example
{
    class EzoGelinSoup : Soup
    {
        public override void AddMaterials()
        {
            Console.WriteLine("Adding Materials for EzoGelin Soup...");
            //I also add some other materials except the main materials
            AddSpecialMaterial("Lentils");
        }

        public override void CookSoup()
        {
            Console.WriteLine("EzoGelin Soup is cooking...");
        }
    }
}

OnionSoup.cs

using System;

namespace TemplatePattern_Example
{
    class OnionSoup : Soup
    {
        public override void AddMaterials()
        {
            Console.WriteLine("Adding Materials for Onion Soup...");
        }

        public override void CookSoup()
        {
            Console.WriteLine("Onion Soup is cooking...");
        }
    }
}

And lastly we have to call these soup subclasses from our program main method. In here I will just declare and initialize the sub classes which derived from Soup abstract classes. And then just call the method which get Soups Ready.

Program.cs

using System;

namespace TemplatePattern_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            TomatoSoup tomSoup = new TomatoSoup();
            tomSoup.GetSoupReady();

            ChickenSoup chickSoup = new ChickenSoup();
            chickSoup.GetSoupReady();

            EzoGelinSoup ezoSoup = new EzoGelinSoup();
            ezoSoup.GetSoupReady();

            OnionSoup onionSoup = new OnionSoup();
            onionSoup.GetSoupReady();

            Console.ReadLine();
        }
    }
}

Below image you can see the output of the example: Template Design Pattern Example Output

That is all in this article.

You can reach the example on Github : https://github.com/thecodeprogram/TemplatePattern_Example

Now you are good to use the Template Design pattern in your projects.

I wish you healthy days.

Burak Hamdi TUFAN.


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment.

Comments

    There is no comment. Be the owner of first comment...