The template method pattern is used to define the skeleton of an algorithm in a method, deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

Applicability

  • When you want to let clients extend only particular steps of an algorithm, but not the whole algorithm or its structure.
  • When you have several classes that contain almost identical algorithms with some minor differences. As a result, you might need to modify all classes when the algorithm changes
  • When you want to control the sequence of steps of an operation

Approach

  1. Analyse the target algorithm to see whether you can break it into steps. Consider which steps are common to all subclasses and which ones will always be unique.
  2. Create the abstract base class and declare the template method and a set of abstract methods representing the algorithm’s steps. Outline the algorithm’s structure in the template method by executing corresponding steps. Consider making the template method final to prevent subclasses from overriding it.
  3. It’s okay if all the steps end up being abstract. However, some steps might benefit from having a default implementation. Subclasses don’t have to implement those methods.
  4. Think of adding hooks between the crucial steps of the algorithm.
  5. For each variation of the algorithm, create a new concrete subclass. It must implement all of the abstract steps, but may also override some of the optional ones.
---
title: Template Method Design Pattern
---
classDiagram
    direction TB
    class Client
    class AbstractClass {
        +templateMethod()
        +step1()
        +step2()
        +step3()
        +step4()
    }
    class ConcreteClassA {
        +step3()
        +step4()
    }
    class ConcreteClassB {
        +step1()
        +step2()
		+step3()
        +step4()
    }

    Client --> AbstractClass
    AbstractClass <|-- ConcreteClassA
    AbstractClass <|-- ConcreteClassB

Example

Identify template method pattern components

Abstract Class: Game
Concrete Classes: Chess, Soccer

Abstract class

Defines the template method and abstract methods for steps.

abstract class Game {
    // Template method
    public final void play() {
        initialize();
        startPlay();
        endPlay();
    }
    
    abstract void initialize();
    abstract void startPlay();
    abstract void endPlay();
}

Concrete class

class Chess extends Game {
    void initialize() {
        System.out.println("Chess Game Initialized! Start playing.");
    }
 
    void startPlay() {
        System.out.println("Chess Game Started. Enjoy the game!");
    }
 
    void endPlay() {
        System.out.println("Chess Game Finished!");
    }
}
 
class Soccer extends Game {
    void initialize() {
        System.out.println("Soccer Game Initialized! Start playing.");
    }
 
    void startPlay() {
        System.out.println("Soccer Game Started. Enjoy the game!");
    }
 
    void endPlay() {
        System.out.println("Soccer Game Finished!");
    }
}

Client

public class Demo {
    public static void main(String[] args) {
        Game game = new Chess();
        game.play();
        System.out.println();
 
        game = new Soccer();
        game.play();
    }
}

Back to parent page: Behavioural Patterns

Design_Pattern Behavioural_Design_Patterns SOFT3202 Template_Method_Pattern