Template Method

Author      Ter-Petrosyan Hakob

The Template Method pattern is a behavioral design pattern that defines the skeleton of an algorithm in a single, final method, deferring specific steps to subclasses. It ensures the overall sequence remains unchanged while allowing subclasses to customize individual steps.

Example: Beverage Preparation

Consider preparing different caffeine beverages (e.g., tea vs. coffee). The algorithm involves these fixed steps:

  1. Boil water
  2. Brew the beverage
  3. Pour into cup
  4. Add condiments

Because the order cannot change (you must boil water before brewing), we implement a final template method that invokes each step in sequence. Subclasses override only the brewing and condiment methods.

public abstract class CaffeineBeverage {

    // The template method defines the fixed algorithm
    public final void prepareRecipe() {
        boilWater();       // common
        brew();            // subclass-specific
        pourInCup();       // common
        addCondiments();   // subclass-specific
    }

    private void boilWater() {
        System.out.println("Boiling water");
    }

    protected abstract void brew();

    private void pourInCup() {
        System.out.println("Pouring into cup");
    }

    protected abstract void addCondiments();
}

Subclasses:

public class Tea extends CaffeineBeverage {
    @Override
    protected void brew() {
        System.out.println("Steeping the tea");
    }

    @Override
    protected void addCondiments() {
        System.out.println("Adding lemon");
    }
}
public class Coffee extends CaffeineBeverage {
    @Override
    protected void brew() {
        System.out.println("Dripping coffee through filter");
    }

    @Override
    protected void addCondiments() {
        System.out.println("Adding sugar and milk");
    }
}

Testing the Pattern:

public class BeverageTest {
    public static void main(String[] args) {
        CaffeineBeverage tea = new Tea();
        CaffeineBeverage coffee = new Coffee();

        System.out.println("Preparing tea:");
        tea.prepareRecipe();

        System.out.println("\nPreparing coffee:");
        coffee.prepareRecipe();
    }
}

Key Points:


Class Diagram

img1

Template Method in the JDK

Important Details

Applicability

Use the Template Method pattern when:

Pros&Cons

Pros:

Cons: