Skip to content
Behavioral

Template Method

Define the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

Intent

Define the overall structure of an algorithm in a base class method while allowing subclasses to override specific steps. The invariant parts of the algorithm stay in the base class, and the variant parts are deferred to subclass implementations.

Problem

You have several classes that contain nearly identical algorithms with only minor differences in certain steps. Duplicating the entire algorithm in each class violates DRY and makes maintenance painful — a bug fix or structural change must be applied to every copy.

Solution

Move the shared algorithm structure into a single template method in an abstract base class. Break the algorithm into steps, making the steps that vary abstract (or overridable). Subclasses implement only the steps that differ, inheriting the fixed structure from the base class. Optional hook methods can provide additional extension points with default no-op behavior.

Participants

  • AbstractClass — defines the template method containing the algorithm skeleton; declares abstract primitive operations that subclasses must implement; may define hook methods with default behavior
  • ConcreteClass — implements the primitive operations to carry out subclass-specific steps of the algorithm

Advantages

  • Eliminates code duplication by pulling the common algorithm structure into a base class
  • Subclasses can override only the specific steps they need to change
  • Inversion of control (Hollywood Principle) — the base class calls subclass methods, not the other way around
  • Hook methods provide optional extension points without requiring subclasses to override them

Disadvantages

  • Relies on inheritance, which can make the design rigid compared to composition-based alternatives
  • Subclasses are constrained by the algorithm structure defined in the base class
  • Can be difficult to understand the flow when there are many steps and hooks
  • Adding new steps to the template method may break existing subclasses

Real-World Analogy

Building a house follows a template method. The overall plan is the same: lay the foundation, build walls, install plumbing, add the roof, and finish interiors. Different house styles (colonial, modern, cabin) follow the same sequence but customize specific steps — the type of foundation, wall materials, and roof design vary. An architect defines the template, and each builder fills in the details.

Use Cases

  • Data parsers — CSV, JSON, and XML parsers share the same parse-validate-transform pipeline but differ in parsing logic
  • Report generators — common structure (header, body, footer) with format-specific rendering
  • Game AI — a turn consists of fixed phases (assess, plan, act) with different logic per character type
  • Build systems — compile, link, and package steps follow the same order but vary per language
  • Test frameworks — setup, execute, verify, teardown lifecycle with user-defined test logic
  • Web scrapers — fetch page, extract data, transform results with site-specific extraction

Code Examples

A data mining framework where the template method defines the fixed pipeline (open, extract, parse, analyze, report) and CsvMiner/JsonMiner implement the format-specific steps.