Template Method Pattern

The template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in a method, called template method. Because of this template method, it is possible for one to redefine certain steps of an algorithm without changing the algorithm's structure.

The intent of this pattern, according to Design Patterns by Gamma et al, is to

  • define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

Most of frameworks nowadays use Template Method extensively by making resuable codes as framework's base classes. This enables the clients of the framework to define customizations by creating derived classes as needed.

 

Structure

In template method pattern, base class declares 'placeholder' for an algorithm, and derived classes implement that placeholders.

To implement this design pattern in object-oriented programming, first a class is created that provides the basic steps of an algorithm. These steps are implemented using abstract methods. Then, in order to implement those abstract methods in real action, they are implemented in subclasses. This results in the general algorithm saved in one place but the concrete steps may be changed by the subclasses.

The structure of the template pattern is shown below:

template pattern

Figure : Template Pattern Structure

Here, AbstractClass contains templateMethod(). The implementation of templateMethod() is: call operation1(), call operation2(), and call operation3(). And  operation1() and operation2() are a placeholder which is declared in the base class, and then defined in derived classes.

 

Example

Let us consider a system for preparing different drinks i.e. tea, coffee et. al. The preparing steps are almost same except adding some ingredients for different drink. The following class diagram explains more about the system.

template method pattern example

Figure: Template Method Pattern example ( Drink Maker)

 

Java Implementation

Here is Java implementation of the example presented above.

First, lets create a DrinkMaker class that is abstract.

/**
 * This is an abstract class that is common to
 * other classes.
 */
public abstract class DrinkMaker {
    /* A template method : */
    public final void makeDrink(){
        System.out.println("Drink will be ready in a while.");
        System.out.println("--------------------------------");
        boilLiquid();
        addIngredients();
        shakeIt();
    }
    abstract void boilLiquid();
    abstract void addIngredients();

    protected void shakeIt(){
        System.out.println("Shake it well. Drink is ready.");
        System.out.println("--------------------------------");
    }
}

Now, lets create concrete classes that implement the abstract class above.

/**
 * We can extend DrinkMaker class in order
 * to create actual drink maker.
 */
public class TeaMaker extends DrinkMaker {
    /* Implementation of necessary concrete methods */
    @Override
    void boilLiquid() {
        System.out.println("Boiling water.");
    }

    @Override
    void addIngredients() {
        System.out.println("Tea and sugar added.");
    }
}
public class CoffeeMaker extends DrinkMaker {
    /* Implementation of necessary concrete methods */
    @Override
    void boilLiquid() {
        System.out.println("Boiling Milk.");
    }

    @Override
    void addIngredients() {
        System.out.println("Coffee and Sugar added.");
    }
}

Now, the client ( Application class in this case) can make any drink within available types as per choice. The following is code example for client.

/**
 * Client
 */
public class Application {
    public static void main(String[] args) {
        DrinkMaker drinkMaker1 = new CoffeeMaker();
        drinkMaker1.makeDrink();

        DrinkMaker drinkMaker2 = new TeaMaker();
        drinkMaker2.makeDrink();
    }
}

The output of the program is:

Drink will be ready in a while.
--------------------------------
Boiling Milk.
Coffee and Sugar added.
Shake it well. Drink is ready.
--------------------------------
Drink will be ready in a while.
--------------------------------
Boiling water.
Tea and sugar added.
Shake it well. Drink is ready.
--------------------------------


Reference:  Head First Design Patterns

Facade Pattern
Composite Pattern