4.10. Template Method

Maheshmaddi
3 min readApr 10, 2023

--

The Template Method pattern is a behavioral design pattern that defines the skeleton of an algorithm in a base class, allowing subclasses to override certain steps of the algorithm without changing the algorithm’s structure. This pattern is useful for promoting code reuse and reducing duplication by deferring the implementation of specific steps to subclasses.

The Template Method pattern is typically used when:

  1. You have an algorithm with multiple steps, and some steps can have different implementations or variations.
  2. You want to avoid duplicating code by reusing common parts of the algorithm in a base class.
  3. You want to provide a framework for extending the algorithm without changing its overall structure.

To implement the Template Method pattern, follow these steps:

  1. Define an abstract base class that contains the skeleton of the algorithm, with methods representing the steps of the algorithm.
  2. Declare the steps that have multiple implementations or variations as abstract methods or provide default implementations in the base class.
  3. Create concrete subclasses that extend the base class, providing specific implementations for the abstract or overridable methods.

Here’s a simple example of the Template Method pattern in Java:

// Abstract base class
abstract class AbstractClass {
// Template method
public void templateMethod() {
step1();
step2();
step3();
}

// Common step
private void step1() {
System.out.println("Performing step 1");
}

// Abstract steps
protected abstract void step2();
protected abstract void step3();
}

// Concrete subclasses
class ConcreteClassA extends AbstractClass {
@Override
protected void step2() {
System.out.println("Performing step 2 in ConcreteClassA");
}

@Override
protected void step3() {
System.out.println("Performing step 3 in ConcreteClassA");
}
}

class ConcreteClassB extends AbstractClass {
@Override
protected void step2() {
System.out.println("Performing step 2 in ConcreteClassB");
}

@Override
protected void step3() {
System.out.println("Performing step 3 in ConcreteClassB");
}
}

// Client code
public class Client {
public static void main(String[] args) {
AbstractClass instanceA = new ConcreteClassA();
instanceA.templateMethod(); // Outputs: step 1, step 2 in ConcreteClassA, step 3 in ConcreteClassA

AbstractClass instanceB = new ConcreteClassB();
instanceB.templateMethod(); // Outputs: step 1, step 2 in ConcreteClassB, step 3 in ConcreteClassB
}
}

In this example, the AbstractClass defines the skeleton of the algorithm, with methods representing the steps of the algorithm. The ConcreteClassA and ConcreteClassB subclasses extend the AbstractClass, providing the specific implementations for the abstract or overridable methods.

Advantages of the Template Method pattern:

  1. Code reuse: The Template Method pattern promotes code reuse by extracting the common parts of the algorithm into a base class, reducing duplication.
  2. Consistent structure: The pattern enforces a consistent structure for the algorithm, making the code easier to understand and maintain.
  3. Open/Closed Principle: The pattern adheres to the Open/Closed Principle, as new variations of the algorithm can be added without modifying the existing code.

Disadvantages of the Template Method pattern:

  1. Inheritance dependency: The Template Method pattern relies on inheritance, which can lead to tight coupling between the base class and the subclasses, making it harder to change the overall structure of the algorithm.
  2. Limited flexibility: The pattern may not be suitable for situations where the algorithm’s structure needs to change dynamically, as the template method defines a fixed sequence of steps.

When using the Template Method pattern, carefully consider its advantages and disadvantages. Use the pattern when you have an algorithm with multiple steps that can have different implementations, and when you want to promote code reuse and maintain a consistent structure. Be aware of the potential limitations introduced by the pattern, such as the inheritance dependency and limited flexibility, and ensure that it is applied judiciously to maintain a clean and efficient codebase.

Note: For complete list of design patterns click here

--

--