BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

Template Method Design Pattern

The template method pattern is a design pattern that allows a group of interchangeable, similarly structured, multi-step algorithms to be defined. Each algorithm follows the same series of actions but provides a different implementation of the steps.

What is the Template Method Pattern?

The template method pattern is a Gang of Four design pattern. This is a behavioural pattern as it defines a manner for controlling communication between classes or entities. The template method pattern is used to define the basic steps of an algorithm and allow the implementation of the individual steps to be changed. This is similar to the strategy design pattern. The key difference is the ability to vary parts of the algorithm rather than replacing the algorithm in its entirety.

The overall structure of the basic algorithm is defined in an abstract base class. This class may include some real functionality but often just defines the order in which the overridable steps will be executed. The implementations for the steps are defined in subclasses. This use of inheritance promotes loose coupling, as the calling function need not know which algorithm is to be executed. Correct use of the pattern also reduces duplication of code.

In the article describing the strategy pattern a scoring system was used as an example. We will use a very similar example for this article, implemented using the template method design pattern. In the game being scored the players run around a circuit that includes checkpoints. At each checkpoint the player throws projectiles at a target, scoring points for each hit. The player's score is reduced if they complete the circuit in a slow time. The algorithms for calculating the score differ according to the sex and age of the player.

Implementing the Template Method Pattern

Template Method Design Pattern UML

The UML class diagram above describes an implementation of the template method design pattern. The items in the diagram are described below:

  • AlgorithmBase. This abstract class is the base class for all concrete versions of the algorithm. The class defines abstract methods for each of the steps that may be adjusted by subclasses. It also includes a single method that controls the algorithm and calls the individual steps. This method, in the diagram named "TemplateMethod", is the one that is called by consumers of the class.
  • ConcreteAlgorithm A/B. The concrete algorithms inherit from the AlgorithmBase class. These algorithms override the abstract step methods to provide real implementations. They do not override the template method.

The following shows the basic code of the template method design pattern implemented using C#.

public abstract class AlgorithmBase
{
    public void TemplateMethod()
    {
        Step1();
        Step2();
        Step3();
    }

    public abstract void Step1();

    public abstract void Step2();

    public abstract void Step3();
}


public class ConcreteAlgorithmA : AlgorithmBase
{
    public override void Step1()
    {
        Console.WriteLine("Algorithm A, Step 1");
    }

    public override void Step2()
    {
        Console.WriteLine("Algorithm A, Step 2");
    }

    public override void Step3()
    {
        Console.WriteLine("Algorithm A, Step 3");
    }
}


public class ConcreteAlgorithmB : AlgorithmBase
{
    public override void Step1()
    {
        Console.WriteLine("Algorithm B, Step 1");
    }

    public override void Step2()
    {
        Console.WriteLine("Algorithm B, Step 2");
    }

    public override void Step3()
    {
        Console.WriteLine("Algorithm B, Step 3");
    }
}
19 July 2009