DEV Community

Juarez Júnior
Juarez Júnior

Posted on

C# Design Pattern: Template Method

The Template Method pattern defines the skeleton of an algorithm in a base class, allowing subclasses to implement specific parts of the algorithm. It’s useful when you have an algorithm that follows a basic structure but some parts may vary depending on the specific implementation. This allows shared code to be reused while still providing flexibility for subclasses.

C# Code Example:

// Abstract class that defines the Template Method
public abstract class PrepareBeverage
{
    // Template Method: defines the steps of the algorithm
    public void Prepare()
    {
        BoilWater();
        AddMainIngredient();
        Serve();
        if (AddCondimentsDesired())
        {
            AddCondiments();
        }
    }

    // Common steps
    private void BoilWater()
    {
        Console.WriteLine("Boiling water...");
    }

    private void Serve()
    {
        Console.WriteLine("Serving the beverage...");
    }

    // Steps to be implemented by subclasses
    protected abstract void AddMainIngredient();
    protected abstract void AddCondiments();

    // Hook method for optional condiments
    protected virtual bool AddCondimentsDesired()
    {
        return true;
    }
}

// Subclass for preparing coffee
public class PrepareCoffee : PrepareBeverage
{
    protected override void AddMainIngredient()
    {
        Console.WriteLine("Adding coffee powder.");
    }

    protected override void AddCondiments()
    {
        Console.WriteLine("Adding sugar and milk.");
    }
}

// Subclass for preparing tea
public class PrepareTea : PrepareBeverage
{
    protected override void AddMainIngredient()
    {
        Console.WriteLine("Adding tea bag.");
    }

    protected override void AddCondiments()
    {
        Console.WriteLine("Adding lemon.");
    }

    protected override bool AddCondimentsDesired()
    {
        return false; // Do not add condiments
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Preparing Coffee:");
        PrepareBeverage coffee = new PrepareCoffee();
        coffee.Prepare();

        Console.WriteLine("\nPreparing Tea:");
        PrepareBeverage tea = new PrepareTea();
        tea.Prepare();
    }
}
Enter fullscreen mode Exit fullscreen mode

Code Explanation:
In this example, the PrepareBeverage class defines the Prepare method, which is the template for preparing a beverage. It follows the same sequence of steps (boil water, add the main ingredient, serve, and optionally add condiments). The PrepareCoffee and PrepareTea subclasses implement the specific details of how to prepare coffee and tea, respectively. The AddCondimentsDesired method is a hook that allows subclasses to decide whether or not condiments will be added.

Conclusion:
The Template Method pattern is useful when you have an algorithm that follows a basic structure, but you want to allow subclasses to modify specific parts of that algorithm. It helps to reuse common code while keeping specialized code separate.

Source code: GitHub

Top comments (0)