Template Method Pattern
The Template Method pattern is a behavioral design pattern in which a base class defines the overall skeleton of an algorithm as a sequence of steps, while deferring the implementation of one or more individual steps to subclasses, letting…
Definition
The Template Method pattern is a behavioral design pattern in which a base class defines the overall skeleton of an algorithm as a sequence of steps, while deferring the implementation of one or more individual steps to subclasses, letting subclasses customize specific parts of an algorithm without changing its overall structure.
Overview
The Template Method pattern is a classic Gang of Four behavioral pattern built around inheritance. A base (abstract) class defines a method — the 'template method' — that lays out an algorithm as a fixed sequence of calls to other methods, some of which are fully implemented in the base class and some of which are left abstract or given a default implementation intended to be overridden by subclasses. The template method itself is typically marked final (or otherwise discouraged from being overridden), so subclasses cannot change the overall sequence of steps, only the content of individual customizable steps. This pattern is a direct application of the Hollywood Principle — 'don't call us, we'll call you' — because the base class controls the flow, calling into subclass-provided methods at the appropriate points, rather than subclasses driving execution themselves. It is one of the most common ways inheritance is used specifically to share algorithmic structure while allowing variation in details, distinguishing it from patterns like Strategy, which achieves similar customization through composition rather than inheritance. A typical example is a data-processing pipeline: a base class defines a `process()` method that calls `readData()`, `parseData()`, `transformData()`, and `saveData()` in a fixed order, where `readData()` and `saveData()` have generic base-class implementations, while `parseData()` and `transformData()` are abstract methods each subclass must implement for its specific data format. Framework code frequently uses the Template Method pattern — for example, defining a request-handling lifecycle where developers override individual hook methods without needing to reimplement the surrounding orchestration logic. Because it relies on inheritance, the Template Method pattern can lead to fragile hierarchies if overused, and many modern codebases favor composition-based alternatives like Strategy or higher-order functions/callbacks where the language supports them, reserving Template Method for cases where the shared algorithmic skeleton itself is a stable, well-understood abstraction.
Key Concepts
- Defines a fixed algorithm skeleton in a base class method
- Defers implementation of specific steps to subclasses via abstract or overridable methods
- Template method itself is typically not overridden, preserving overall step order
- Embodies the Hollywood Principle: base class calls into subclass hooks
- Achieves code reuse of shared algorithmic structure via inheritance
- Commonly used in frameworks to define extensible lifecycles
- Distinguished from Strategy pattern by using inheritance rather than composition
- One of the original 23 Gang of Four design patterns