Behavioral pattern · Visual logic
Template Method: Freeze the Sequence, Vary the Steps
Template Method defines a stable algorithm sequence while allowing selected steps to vary through subclass hooks or overrides.
Several workflows share the same sequence but duplicate orchestration because a few steps differ by case.
Keep the invariant algorithm in one template method and expose only intentional variation points to specialized implementations.
- TEMPLATE executes the shared sequence in a controlled order.
- FIXED STEPS enforce invariants that every workflow must preserve.
- HOOKS let specialized implementations vary selected operations only.
Related workflows reuse one verified sequence while customizing only the parts designed to change.
Run it in the real world
A data import pipeline always validates, parses, normalizes, and stores, while CSV and JSON importers override only parsing.
Go deeper
This is an original explanation and example inspired by Head First Design Patterns. The book covers the complete pattern, design principles, and implementation detail.
Retail links are not affiliate links yet. If that changes, this page will clearly disclose it.