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.
public abstract class DataProcessor { // template method — final so subclasses cannot break the order public final void process() { readData(); processData(); saveData(); } private void readData() { System.out.println("Reading data..."); } // the step that varies protected abstract void processData(); private void saveData() { System.out.println("Saving data..."); }}public class CsvProcessor extends DataProcessor { protected void processData() { System.out.println("Processing CSV data"); }}public class XmlProcessor extends DataProcessor { protected void processData() { System.out.println("Processing XML data"); }}// useDataProcessor processor = new CsvProcessor();processor.process();
// No inheritance in Go — the template becomes a function that// takes the varying step as an interface.type DataSteps interface { ProcessData()}func Process(steps DataSteps) { fmt.Println("Reading data...") steps.ProcessData() // the step that varies fmt.Println("Saving data...")}type CsvProcessor struct{}func (CsvProcessor) ProcessData() { fmt.Println("Processing CSV data")}type XmlProcessor struct{}func (XmlProcessor) ProcessData() { fmt.Println("Processing XML data")}// useProcess(CsvProcessor{})
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.