Unravel โบ Java concepts โบ Design patterns and architecture
Template method pattern
A base class defines the fixed skeleton of an algorithm in a final method and leaves some steps as abstract or overridable hooks that subclasses fill in.
Learn these first
At a glance
A cooking class with a fixed lesson plan (prep, cook, plate) where every student chooses only the recipe for the cook step.
- 1public final void run() { open(); try { doWork(); } finally { close(); } } fixes the order and the clean-up; protected abstract void doWork(); is the step subclasses implement
- 2Optional hooks are empty or default methods that subclasses may override
- 3JDK and framework examples: AbstractList, HttpServlet.service calling doGet and doPost, Spring's JdbcTemplate callbacks, JUnit's @BeforeEach/@AfterEach lifecycle
- 4Alternative with less inheritance: pass the varying step as a lambda (run(() -> work())), which is the strategy pattern
- Mark the template method final so subclasses cannot break the order
- It inverts control ("don't call us, we call you"): the base calls the subclass, which can be surprising to read
- A normal virtual call from the superclass method: doWork() is dispatched to the subclass override through the method table at run time
public abstract class Job { public final void run() { System.out.println("start"); try { doWork(); } finally { System.out.println("done"); } } protected abstract void doWork();}class ReportJob extends Job { @Override protected void doWork() { System.out.println("building report"); }}Read the full explanation (about 1 min)
A base class defines the fixed skeleton of an algorithm in a final method and leaves some steps as abstract or overridable hooks that subclasses fill in. The base controls the order; the subclasses supply the details.
A mental model
A cooking class with a fixed lesson plan (prep, cook, plate) where every student chooses only the recipe for the cook step.
How it works
public final void run() { open(); try { doWork(); } finally { close(); } } fixes the order and the clean-up; protected abstract void doWork(); is the step subclasses implement.
Optional hooks are empty or default methods that subclasses may override.
JDK and framework examples: AbstractList, HttpServlet.service calling doGet and doPost, Spring's JdbcTemplate callbacks, JUnit's @BeforeEach/@AfterEach lifecycle.
Alternative with less inheritance: pass the varying step as a lambda (run(() -> work())), which is the strategy pattern.
Going deeper
Mark the template method final so subclasses cannot break the order. Do not call overridable methods from the base class constructor.
It inverts control ("don't call us, we call you"): the base calls the subclass, which can be surprising to read. Document which methods subclasses must and may override.
Under the hood
A normal virtual call from the superclass method: doWork() is dispatched to the subclass override through the method table at run time.
Common mistakes
- A non-final template method that subclasses override and break.
- Too many hooks to understand the flow.
- Calling abstract methods in the constructor.
How Unravel recognises it
When you paste code, Unravel looks for these signs of template method pattern. Spotting them yourself is a good habit when you read unfamiliar code:
- Method names you declare:
execute()process()doExecute()doProcess()doRun()
Part of a learning path
- Design patterns and SOLID ยท previous: Visitor pattern ยท next: DAO and repository pattern
Related concepts
Frequently asked questions
What is Template method pattern?
A base class defines the fixed skeleton of an algorithm in a final method and leaves some steps as abstract or overridable hooks that subclasses fill in. The base controls the order; the subclasses supply the details.
What are the most common mistakes with Template method pattern?
A non-final template method that subclasses override and break. Too many hooks to understand the flow. Calling abstract methods in the constructor.
How can I see Template method pattern in real code?
Paste your own Java into Unravel and it will point out where template method pattern appears, explain how it works at the detail level you choose, and draw the flow of the code around it.