The Template Method Pattern is a behavioral design pattern that defines the skeleton of an algorithm in a base class while allowing subclasses to customize specific steps without changing the overall algorithm.

The common workflow is implemented once in the abstract class, while individual steps are delegated to abstract or overridable methods that subclasses provide.

This ensures that all subclasses follow the same sequence of operations while allowing variations in selected parts of the algorithm.

The Template Method Pattern is commonly used in frameworks, file processing, report generation, data import/export, and application lifecycle management.

Structure

The Template Method Pattern consists of the following participants.

1. The Abstract Class defines the template method that represents the overall algorithm.
2. The Template Method invokes individual steps in a fixed order.
3. Concrete Classes implement or override the customizable steps of the algorithm.
4. The Client invokes the template method without knowing the implementation details.

Java Implementation

Suppose an application imports different types of files. Every import follows the same workflow of opening the file, processing its contents, and closing the file, but the processing logic differs depending on the file type.

Create the abstract class.
public abstract class FileImporter {
    public final void importFile() {
        openFile();
        processFile();
        closeFile();
    }

    private void openFile() {
        System.out.println("Opening file.");
    }

    protected abstract void processFile();

    private void closeFile() {
        System.out.println("Closing file.");
    }
}
Create the concrete classes.
public class CsvImporter extends FileImporter {
    @Override
    protected void processFile() {
        System.out.println("Processing CSV file.");
    }
}
public class JsonImporter extends FileImporter {
    @Override
    protected void processFile() {
        System.out.println("Processing JSON file.");
    }
}
The client invokes the template method.
public class Main {
    public static void main(String[] args) {
        FileImporter importer = new CsvImporter();
        importer.importFile();
    }
}
Output:
Opening file.
Processing CSV file.
Closing file.

How It Works?

The client works only with the FileImporter abstraction and invokes the importFile() template method.

The template method defines the complete workflow by calling openFile(), processFile(), and closeFile() in a fixed sequence.

The common steps are implemented once in the abstract class, while the processFile() method is left abstract for subclasses to implement. Each concrete importer provides its own processing logic without affecting the overall workflow.

This guarantees that every file import follows the same sequence of operations while allowing individual processing steps to vary.

Advantages

1. It promotes code reuse by placing common workflow in a base class.
2. It enforces a consistent algorithm across all subclasses.
3. Subclasses customize only the required steps of the algorithm.
4. It reduces duplicate code and improves maintainability.

Disadvantages

1. The pattern relies on inheritance, creating tighter coupling between the base class and subclasses.
2. Changes to the template method may affect every subclass.
3. It is less flexible than composition-based patterns such as Strategy.

JDK Examples

The Template Method Pattern appears throughout the Java ecosystem.

1. The java.io.InputStream class defines common stream operations while subclasses such as FileInputStream and ByteArrayInputStream provide specific implementations.

2. The java.util.AbstractList class provides common implementations of list operations while concrete classes such as ArrayList and LinkedList implement storage-specific behaviour.

Summary

The Template Method Pattern defines the overall structure of an algorithm in a base class while allowing subclasses to customize selected steps.

By implementing the common workflow once and delegating variable behaviour to subclasses, the pattern promotes consistency, reuse, and maintainability across related implementations.
Nagesh Chauhan

Nagesh Chauhan

Principal Software Engineer • Java • Python • Distributed Systems • AI/ML

Principal Software Engineer with 14+ years of experience designing and delivering large-scale distributed systems, cloud-native applications, and AI-powered platforms.

Passionate about solving complex engineering problems using strong data structures and algorithms, along with expertise in Java, Spring Boot, Python, System Design, Microservices, Cloud, Kafka, Elasticsearch, and Generative AI.

Share this Article

💬 Comments

Join the Discussion