Template Method: a skeleton with gaps
- Lesson 23 of 32
- 10 min
- Intermediate
- 12 steps
Two reports with the same steps copied. You will move the skeleton up to a base class, leave gaps for the subclasses and see when a function does the same with less.
- Copies of the skeleton
- 1
- request
- response
Step 1 of 12
Problem
Two reports, the same skeleton
An app builds text reports. SalesReport and StockReport build their output the same way: a REPORT header, the rows and a -- end -- footer. Only the rows differ.
Each class writes those three steps on its own. The order and the format are copied, and nothing forces them to stay the same.
Step 2 of 12
Problem
You change the footer and miss one
You are asked to make the footer read -- end of report --. You open SalesReport and change it. But StockReport has its own copy, and that one keeps the old footer.
Nothing warns you: both classes compile and run. The reports no longer match, and whoever reads them finds out.
What changed
- SalesReport footer
Step 3 of 12
Concept
The skeleton moves up to a base class
We move the steps into an abstract class, Report. Its render() method fixes the order: header, rows, footer. That method is the template method: the skeleton of the algorithm.
The part that varies, rows(), stays as a gap: an abstract method each subclass fills in. SalesReport and StockReport inherit from Report; the line with a hollow triangle means “inherits from”.
What changed
- abstract class Report
- 2 copies of the skeleton
Step 4 of 12
Technique
You do not call your part: it gets called
SalesReport only writes rows(). Your code calls render(), which lives in Report, and render() is the one that calls rows() when its turn comes.
That is inversion of control: the base class decides when your part runs. It is the shape you will meet in frameworks: you extend one of their classes, write a method and the framework calls it.
Step 5 of 12
Technique
Steps with a default and required steps
Not every gap is the same. header() and footer() come with a default: the subclass may override them. Those optional steps are called hooks. rows() is abstract: the subclass must write it, or it does not compile.
StockReport overrides header() to start with STOCK; SalesReport uses the base one. The order of the steps stays in render(), and no subclass touches it.
What changed
- header() in StockReport
Step 6 of 12
Solution
A new report is one method
UsersReport arrives. You no longer copy the skeleton: you extend Report and write rows(). Header, footer and order come from the base.
And the footer from step 2 now changes in one place: footer() in Report. All three reports get it.
What changed
- UsersReport
- footer() in Report
Step 7 of 12
Technique
In Java: `AbstractList`
Java’s standard library uses this pattern. For a read-only list you extend AbstractList and write only get(int) and size().
The rest is already built on those two methods: iterator() and indexOf(), for example. You fill two gaps; the base brings the algorithm.
Step 8 of 12
Technique
In Java: `InputStream`
Another case: to build your own InputStream you must write read(), which returns the next byte, or -1 when there are no more.
The base’s read(buf, off, len) calls read() again and again to fill the array. You say how to read one byte; the base decides how many times to ask.
Step 9 of 12
Concept
Not HTML templates, and inheritance is optional
Two common mix-ups. The template in this pattern has nothing to do with the HTML templates of a view engine: it is an algorithm with fixed steps and gaps.
And the idea does not need inheritance: if the gap is a function you receive as a parameter, you get the same with less coupling. You will see it in the next step.
Step 10 of 12
Technique
The same skeleton without inheritance
renderReport receives the step that varies as a function, rows. It fixes the order just like render(), but there is no base class and no subclasses: you call it and pass your part.
This is the idea behind Strategy, the first lesson of this module. In your own code it is usually the simplest option; inheritance makes sense when a framework already gives you the base class.
What changed
- extends Report
- rows: () => string[]
Step 11 of 12
Trade-off
When not to use Template Method
You gain a skeleton nobody can break and steps that are not repeated. You pay with coupling: every subclass depends on how Report calls its methods, and a change in the base reaches all of them.
Avoid it when a function is enough, or when subclasses need to change the order of the steps: the pattern exists precisely to fix it. And whoever reads SalesReport sees rows() but not who calls it; they have to look for it in the base.
Step 12 of 12
Summary
The base fixes the order, you fill the gaps
When several classes repeat the same steps with different details, move the skeleton into a template method in a base class and leave the details as methods the subclasses fill in.
You will meet it in frameworks and in Java (AbstractList, InputStream). In your own code, if passing a function is enough, pass a function.
← → step · R replay · Space pause
You will learn
- Spot the problem: the same steps copied into several classes that differ in one detail. (covered)
- Move the skeleton into a base class method that calls the steps that vary. (covered)
- Tell steps with a default apart from steps the subclass must write. (covered)
- Recognise the pattern in Java’s `AbstractList` and `InputStream`. (covered)
- Know when a function parameter is simpler and when the pattern does not fit. (covered)