creational
Template Method
Define the skeleton of an algorithm in a base class, deferring specific steps to subclasses.
Several classes frequently run the same overall procedure while differing only in a few steps. Data miners all open a source, extract, parse, analyse, and report — but a CSV miner and a PDF miner diverge only in how they open and read. Copy-pasting the shared skeleton into each subclass duplicates the structure and lets the copies drift apart over time. Template Method captures the invariant outline once and lets subclasses supply only the variable parts.
A recipe — the process is always prep → cook → plate. What you prep, cook, and plate varies by dish.
Key Concepts
1
The base class defines a template method — typically made final so subclasses can't disturb the sequence — that calls a fixed series of steps in order. Some of those steps are abstract and must be implemented by each subclass; others are concrete defaults, called hooks, that a subclass may optionally override to adjust or extend behaviour at chosen points. Control stays with the base class; this is the Hollywood Principle, "don't call us, we'll call you." The subclass fills in blanks without ever seeing or reordering the whole.
final
2
It suits families of classes that share an algorithm's flow but differ in detail, situations where a fixed sequence must be enforced, and framework lifecycles where the framework owns the order and users implement the steps. The contrast to draw in an interview is with Strategy: Template Method uses inheritance and a fixed structure, varying individual steps, whereas Strategy uses composition to swap an entire algorithm at runtime. Prefer Strategy when the whole algorithm changes; prefer Template Method when only certain steps do. The cost is that inheritance is rigid — a subclass is bound to one base, and a deep hierarchy of overrides can be hard to follow.