Template Method Design Pattern Explained
Template Method defines the overall skeleton of an algorithm in a base class, deferring specific steps to subclasses without letting them change the algorith...
Published November 13, 2026
The Template Method pattern defines the skeleton of an algorithm in a base class and lets subclasses override specific steps without changing the overall order.
The concept
The base method calls a fixed sequence: step1(), step2(), step3(). Some steps are abstract or have default implementations that subclasses override.
It enforces a consistent workflow while allowing variation, such as report generation or data import pipelines.
Fixed order, customisable steps
- run() starts the algorithm
- readData() — subclass specific
- transform() — shared default
- save() — subclass specific
Practical examples
Base class
class Importer {
run() { const d = this.read(); const t = this.transform(d); this.save(t) }
transform(d) { return d }
read() { throw new Error('implement') }
save() { throw new Error('implement') }
}Subclass
class CsvImporter extends Importer {
read() { return readCsv('in.csv') }
save(rows) { db.insert(rows) }
}
new CsvImporter().run()Hook method
class A { run() { this.before?.(); this.work(); this.after?.() } }Common mistakes
- Too many overridable steps.
- Subclasses calling steps in a different order.
- Preferring inheritance when composition (Strategy) is simpler.
Key takeaways
- Skeleton in base, steps in subclasses.
- Fixed order.
- Consider Strategy as an alternative.
FAQ
Template Method vs Strategy?
Template uses inheritance; Strategy uses composition.
Where is it common?
Frameworks with lifecycle hooks, like test runners.