Template Method Pattern in C#

The Template Method Pattern defines the skeleton of an algorithm in a base class but lets subclasses change specific steps. It allows code reuse for the overall structure while giving flexibility to customise parts of the process.

Real-Life Analogy: Baking a Cake from a Recipe

Think about a cake recipe. It tells you what steps to follow: prepare ingredients, bake the cake, and decorate. Every baker follows the same structure, but how they decorate — or which ingredients they use — may vary.

The recipe is the template. Each baker is a subclass, choosing how to implement the flexible parts while keeping the baking process the same. That's how the Template Method Pattern works.

Cake being made with fixed steps and custom decoration
Like baking a cake with a standard recipe but custom ingredients or decoration, the Template Method Pattern defines a structure with overridable steps.

Benefits of Template Method

The Template Method Pattern promotes reuse and consistency. It ensures all subclasses follow the same flow while giving them the power to customise key parts.

  • Defines a clear algorithm flow
  • Avoids code duplication by reusing the structure
  • Supports variation where it matters

When Should You Use It?

Use this pattern when you have a clear series of steps, and each variation follows the same structure. The Template Method Pattern helps enforce consistency while still allowing flexible behaviour.

It's useful when:

  • You have several processes that follow the same overall sequence
  • You want to avoid duplicating logic across classes
  • You want to restrict how subclasses can alter the flow

When Not to Use It: Avoid this pattern if subclasses need to change the overall flow, or if enforcing the structure becomes too limiting for your use case.

What to Implement

To implement the Template Method Pattern, you need:

  • Abstract Class: Contains a method that defines the full algorithm (the template method)
  • Concrete Subclasses: Provide specific implementations for the steps defined in the base class
  • Optional Hooks: Base methods that can optionally be overridden

This structure lets you enforce a standard process while allowing variations in individual steps. Subclasses don’t change the flow — only how some parts behave.

How It Works in C#

We'll model a baking process. The base class defines the steps for making a cake — some are fixed, others must be implemented by the subclass.


// Abstract Class (Template)
public abstract class CakeRecipe
{
    // Template Method
    public void BakeCake()
    {
        PrepareIngredients();
        Bake();
        Decorate();
    }

    protected abstract void PrepareIngredients();
    protected virtual void Bake() => Console.WriteLine("Baking at 180°C for 30 minutes");
    protected abstract void Decorate();
}

// Concrete Class
public class ChocolateCake : CakeRecipe
{
    protected override void PrepareIngredients()
    {
        Console.WriteLine("Mixing flour, eggs, cocoa powder, sugar...");
    }

    protected override void Decorate()
    {
        Console.WriteLine("Decorating with chocolate frosting and sprinkles");
    }
}

public class FruitCake : CakeRecipe
{
    protected override void PrepareIngredients()
    {
        Console.WriteLine("Mixing flour, eggs, dried fruit, honey...");
    }

    protected override void Decorate()
    {
        Console.WriteLine("Topping with glazed fruits and almonds");
    }
}

Client usage:


var chocolate = new ChocolateCake();
chocolate.BakeCake();

var fruit = new FruitCake();
fruit.BakeCake();

Final Thoughts

The Template Method Pattern is like following a cake recipe. You always prepare, bake, and decorate — but how you do each step is up to you.

It helps define consistent structure while still allowing creativity and variation in the details.