08 Jan 2025

Template Method Pattern

Overview

The Template Method Pattern is a behavioral design pattern that defines the skeleton of an algorithm in a base class but allows subclasses to provide specific implementations for certain steps. This pattern promotes code reuse and ensures the overarching structure of the algorithm remains unchanged.

Key Participants

  1. Abstract Class

  • Defines the template method, which contains the skeleton of the algorithm.

  • Implements some steps of the algorithm and declares abstract methods for steps to be implemented by subclasses.

  1. Concrete Class

  • Implements the abstract methods declared in the abstract class.

  • Provides specific behavior for steps of the algorithm.

Implementation in Code

Example: Preparing Beverages

// Abstract Class
abstract class Beverage {
    // Template method
    public final void prepareRecipe() {
        boilWater();
        brew();
        pourInCup();
        addCondiments();
    }

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

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

    // Abstract methods to be implemented by subclasses
    protected abstract void brew();
    protected abstract void addCondiments();
}

// Concrete Class: Tea
class Tea extends Beverage {
    @Override
    protected void brew() {
        System.out.println("Steeping the tea");
    }

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

// Concrete Class: Coffee
class Coffee extends Beverage {
    @Override
    protected void brew() {
        System.out.println("Dripping coffee through filter");
    }

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

// Client
public class TemplateMethodPatternDemo {
    public static void main(String[] args) {
        Beverage tea = new Tea();
        System.out.println("Preparing tea:");
        tea.prepareRecipe();

        System.out.println("\nPreparing coffee:");
        Beverage coffee = new Coffee();
        coffee.prepareRecipe();
    }
}

Key Methods

  1. templateMethod()

  • Defines the fixed structure of the algorithm.

  • Calls concrete or abstract methods implemented by subclasses.

  1. Concrete and Abstract Methods

  • Subclasses override abstract methods to provide specific behavior for certain steps.

Advantages

  1. Code Reuse: The template method defines common steps in one place, reducing code duplication.

  1. Consistency: Ensures that the algorithm’s structure remains consistent across different implementations.

  1. Flexibility: Subclasses can customize specific steps without altering the overall algorithm.

Disadvantages

  1. Restricted Flexibility: The overall structure of the algorithm cannot be changed by subclasses.

  1. Dependency on Abstract Class: Subclasses must follow the structure defined by the base class, which may lead to rigidity.

Real-World Examples

  1. Frameworks: Frameworks use the template method to enforce a specific workflow while allowing developers to override specific steps.

  1. Data Processing: Algorithms for reading, processing, and saving data can use the template method to define the sequence of operations.

  1. Game Development: Game engines use the template method to define the lifecycle of a game (e.g., initialize, update, render, terminate).

When to Use the Template Method Pattern?

  • When multiple classes share a common structure in their algorithms but require variations in some steps.

  • When you want to enforce a consistent structure for an algorithm across different implementations.

  • When code reuse and reducing duplication are essential.

← Back to Library