Introduction

Template method design pattern is to define an algorithm as skeleton of operations and leave the details to be implemented by the child classes. The overall structure and sequence of the algorithm is preserved by the parent class. This design pattern is used popularly in framework development. This helps to avoid code duplication also.

The component designer decides which steps of an algorithm are invariant (or standard), and which are variant (or customizable). The invariant steps are implemented in an abstract base class, while the variant steps are either given a default implementation, or no implementation at all. The variant steps represent “hooks”, or “placeholders”, that can, or must, be supplied by the component’s client in a concrete derived class. The component designer mandates the required steps of an algorithm, and the ordering of the steps, but allows the component client to extend or replace some number of these steps.

Intent

  • 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.
  • Base class declares algorithm ‘placeholders’, and derived classes implement the placeholders.

Implementation

AbstractClass defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects. ConcreteClass implements the primitive operations to carry out subclass-specific steps of the algorithm. When a concrete class is called the template method code will be executed from the base class while for each method used inside the template method will be called the implementation from the derived class.

Example

A travel agency is managing each trip.  All the trips contain common behavior but there are several packages. Each trip contains the basic steps:

  • The tourists are transported to the holiday location by plane/train/ships.
  • Each day they are visiting something
// Trip planner desing using Template design pattern
public class Trip {
  public final void performTrip(){
    doComingTransport();
    doDayA();
    doDayB();
    doDayC();
  }
  
  public abstract void doComingTransport();
  public abstract void doDayA();
  public abstract void doDayB();
  public abstract void doDayC();
}

public class PackageA extends Trip {
  public void doComingTransport() {
    System.out.println("Comming by air");
  }
  public void doDayA() {
    System.out.println("Visiting the aquarium");
  }
  public void doDayB() {
    System.out.println("Going to the beach");
  }
  public void doDayC() {
    System.out.println("Going to mountains");
  }
}

public class PackageB extends Trip {
  public void doComingTransport() {
    System.out.println("Comming by train");
  }
  public void doDayA() {
    System.out.println("Visiting the mountain");
  }
  public void doDayB() {
    System.out.println("Going to the beach");
  }
  public void doDayC() {
    System.out.println("Going to zoo");
  }
}