advanced
Template Method
Define an invariant algorithm skeleton while letting subclasses or hooks customize selected steps.
Template Method defines an invariant algorithm skeleton in a base class while subclasses override selected steps through hooks or abstract methods—ETL pipelines, test setup/teardown, request handling with shared logging. It favors inheritance-based extension, so prefer composition when variation is runtime-configurable.
On interviews: compare with Strategy and with pipeline functions. Discuss fragile base class problem and hook ordering.
Common pitfalls: subclasses breaking skeleton assumptions; hidden hooks called in surprising order; template methods that grow into monoliths.
The trade-off is flexibility versus complexity—know when the simpler path is enough.
Checklist:
- Skeleton steps are fixed and documented.
- Hooks have safe default no-op behavior.
- Subclasses cannot skip mandatory invariants.
- Consider Strategy when runtime swapping is needed.