The Template Method pattern defines the skeleton of an algorithm in a base class and lets subclasses override specific steps without changing the algorithm's structure.

Intent

Customize the implementation of an algorithm while keeping the overall flow consistent. The pattern fits when you need several variations of an algorithm that share common logic.

Problem

An algorithm has a fixed order of steps, but a few steps differ between implementations. Inheritance can duplicate the shared part, and tight coupling makes the variants hard to change. The Template Method pattern fixes this:

  • One structure. The overall flow stays consistent across all implementations.
  • Shared code in one place. Common logic is written once in the base class.
  • Small override surface. Subclasses override only the parts that differ.
  • Composition when you want it. You can pass callable objects instead of inheriting.

Solution

Define an abstract base class (or interface) with:

  1. A template method that outlines the algorithm's steps
  2. Abstract methods or callback slots for customizable steps
  3. Concrete implementations that plug in custom behavior

Modern approaches use composition and pass callables (std::function in C++, callables in Python) through the constructor. That is more flexible than traditional inheritance.

Key participants

  • AbstractClass: defines the template method and the abstract or customizable steps
  • ConcreteClass: implements the customizable steps
  • Algorithm Consumer: uses the class and can provide custom implementations

Advantages

  • Reduces code duplication by extracting common algorithm logic
  • Makes the algorithm's structure explicit and reusable
  • Separates concerns cleanly
  • Works with both inheritance and composition
  • Lets you test each algorithm step in isolation

Disadvantages

  • Adds an unnecessary base class when only one implementation exists
  • Complex algorithm hierarchies can become hard to maintain
  • Clients must understand the algorithm's steps and call points

Implementation approaches

Approach 1: traditional inheritance

Define abstract methods in the base class that subclasses must implement.

Approach 2: composition with callables (modern)

Pass callback functions or function objects through the constructor. This approach is more flexible and avoids inheritance hierarchies.

Pattern structure

This UML class diagram shows the traditional Template Method structure:

Mermaid diagram

Key elements:

  • AbstractTemplate: holds the template method that orchestrates the algorithm's steps
  • primitiveOperation1-3: abstract methods (customizable steps) that subclasses must implement
  • ConcreteImplementationA/B: provide specific implementations of the abstract methods
  • The algorithm structure is fixed in the base class; only the details change

Modern implementations often use composition with functions instead of inheritance, which makes the pattern more flexible without changing its core idea.

Summary

The pattern is small. One non-virtual function fixes the order, and virtual functions or callables fill in the steps. Use inheritance when the steps belong to a type. Use callables when callers need to swap behavior without creating a subclass.

Examples

Implementations in different languages: