WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Design Patterns · Basic · question 19 of 100

What is the Template Method pattern, and how does it encourage code reusability?

📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

The Template Method pattern is a behavioral design pattern that defines a skeleton of an algorithm in a base class, but lets subclasses override specific steps of the algorithm without changing its structure. The idea behind this pattern is to define a template containing the steps of a certain algorithm, then let the subclasses customize some of those steps as needed, without modifying the overall algorithm structure. In other words, the template method pattern defines an algorithm at a high level, but allows variations at the low level.

The template method pattern is useful when you want to implement an algorithm once, but allow for some parts of it to be customized by subclasses. This can help avoid duplication of code and increase code reusability. By providing a standard implementation of the algorithm and allowing customization of specific steps, subclasses can focus on adding or modifying functionality in a consistent manner.

To implement the template method pattern, we first declare an abstract class that defines the structure of the algorithm. Within this class, we define the template method that contains the overall algorithm structure, calling on other methods which can be overridden by subclasses. These methods are called "hooks" because they provide points where the algorithm can be customized. The subclasses then implement these hooks to provide their own functionality, while leaving the rest of the algorithm intact.

Here is an example implementation in Java:

public abstract class AlgorithmTemplate {
    
    public void executeAlgorithm() {
        step1();
        step2();
        step3();
    }
    
    protected abstract void step1();
    
    protected void step2() {
        // default implementation
    }
    
    protected abstract void step3();
}

public class SubAlgorithm extends AlgorithmTemplate {
    
    protected void step1() {
        System.out.println("Step 1 of SubAlgorithm");
    }
    
    protected void step3() {
        System.out.println("Step 3 of SubAlgorithm");
    }
}

public class AnotherSubAlgorithm extends AlgorithmTemplate {
    
    protected void step1() {
        System.out.println("Step 1 of AnotherSubAlgorithm");
    }
    
    protected void step2() {
        System.out.println("Custom Step 2 of AnotherSubAlgorithm");
    }
    
    protected void step3() {
        System.out.println("Step 3 of AnotherSubAlgorithm");
    }
}

// Usage:
AlgorithmTemplate algorithm = new SubAlgorithm();
algorithm.executeAlgorithm();

AlgorithmTemplate anotherAlgorithm = new AnotherSubAlgorithm();
anotherAlgorithm.executeAlgorithm();

In this implementation, the ‘AlgorithmTemplate‘ class defines the overall algorithm structure with the ‘executeAlgorithm()‘ method. This method calls on the ‘step1()‘, ‘step2()‘, and ‘step3()‘ methods, which are defined as "hooks" that can be overridden by subclasses. The ‘SubAlgorithm‘ and ‘AnotherSubAlgorithm‘ classes then implement these hooks to provide their own functionality.

By using the template method pattern, we can define a standard algorithm structure in the ‘AlgorithmTemplate‘ class, while allowing for customization in the subclasses. This can make our code more modular and maintainable, enabling us to reuse the same algorithm in different contexts.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Design Patterns interview — then scores it.
📞 Practice Design Patterns — free 15 min
📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

All 100 Design Patterns questions · All topics