Template Method Pattern

What is the Template Method pattern?

Template Method means: a base class fixes the order of steps in an algorithm, and lets subclasses fill in only the steps that vary.

The base method is the “template.” It calls other methods in a fixed order. Some of those are shared by everyone; others are overridden by each subclass. In plain words: the parent says “first do A, then B, then C — you may customize B.” Children never reorder the steps; they only supply the parts that differ.

This is normally an inheritance pattern — the variation happens by subclassing. Go has no classical inheritance, so its version below uses composition instead: a shared function takes the varying step as an interface. That is closer in spirit to Strategy, but the intent — one fixed skeleton, one swappable step — stays the same.


The problem it solves

Without a template, two exporters often look like twins with a few lines changed.

load and write drift over time. Someone adds validation to one copy and forgets the other. Template Method makes the shared sequence real code in one place.


The template method and the step that varies

The caller runs one method. The base guarantees the sequence. The subclass (or, in Go, the injected step) only implements the one part that changes.


A shop-style exporter

The same idea with a tiny catalog export — both exporters share load and write, only transform changes.


Hooks: optional steps with a default

Sometimes a step is optional. The base provides a default (often “do nothing”) that subclasses may override — usually called a hook.

A subclass that needs backups overrides the hook to return true. Everyone else inherits the default and stays simple.


Template Method vs Strategy

Both remove duplication around “same idea, different details.”

Template Method uses inheritance. The skeleton lives in the parent. Use it when the order of steps is an invariant you want to protect.

Strategy uses composition. You inject a behavior object. Use it when you want to swap algorithms at runtime without growing a class hierarchy.

If you find yourself with deep inheritance just to tweak one step, Strategy (or a plain function) is often healthier.


When it helps

Use it when several classes share a multi-step workflow and you want one place that defines the sequence — data import pipelines, report generation, “setup → run → teardown” flows.

It also helps when you want to stop others from reordering critical steps: mark the template method final (or, in Go, keep the skeleton function as the only entry point).


When it hurts

Inheritance couples subclasses to the parent. A clumsy base class becomes a bottleneck, and if subclasses need to radically change the flow, the template is the wrong tool.

Also avoid templates that expose many abstract methods “just in case.” Each abstract step is a burden. Prefer a short skeleton and a few clear hooks.


Practical tips

  • Make the template method final (or otherwise locked) when the sequence must not change.
  • Keep shared steps in the base; keep varying steps abstract or hooked.
  • Name steps so the algorithm reads like a checklist (load, transform, write).
  • Prefer fewer inheritance layers — this pattern is not an excuse for deep hierarchies.
  • If runtime swapping matters more than a fixed skeleton, switch to Strategy.

Check your understanding

Template Method in one line?
A base method runs a fixed algorithm and lets subclasses fill in selected steps.

Why lock the template method?
So subclasses cannot break the agreed order of steps.

What is a hook?
An optional overridable step with a safe default in the base.

Template Method or Strategy?
Fixed skeleton and inheritance → Template Method. Swappable behavior and composition → Strategy.


What to learn next

You have finished the course. Revisit earlier chapters any time, or pick a pattern from the syllabus to reinforce.