Template Method Pattern
Template Method: start from the problem
See the responsibilities
Two importers follow the same read, parse and save sequence, but parse different formats. Duplicating that sequence in each importer makes ordering rules diverge.
This is a behavioral pattern lesson. Follow the responsibilities, inspect the small JavaScript example and compare the design choices. The example is local teaching code; its boundary is explained before you apply the idea to a real service.
What you’ll learn
- Explain the problem Template Method addresses and the roles involved.
- Follow the example and distinguish this pattern from nearby alternatives.
- Choose when the benefit justifies the extra responsibilities and testing.
How the responsibilities work
Template Method puts the algorithm’s overall sequence in a base operation and allows selected steps to vary through hooks or overrides. The stable sequence states which steps occur and in what order. Subclasses should only customize the intended variation.
The importer example keeps run in the base class and changes parse for CSV. The example is synchronous; external storage errors and transactional requirements need their own contract.
Follow a small example
Read the JavaScript from top to bottom and compare the expected output. The values are chosen to make this pattern’s responsibility visible. This example runs in one local execution context; it does not create remote services, durable storage or production access controls.
JavaScript · local teaching example
class Importer {
read() { return 'red,blue'; }
parse(text) { throw new Error('Choose a parser'); }
save(items) { return items.join(' | '); }
run() { return this.save(this.parse(this.read())); }
}
class CsvImporter extends Importer {
parse(text) { return text.split(','); }
}
console.log(new CsvImporter().run());Expected output
red | blueChoose a design and inspect the consequence
Compare the design choices
Compare the visible consequences. With JavaScript enabled, choose an option in each scenario to assemble your decision record.
This is an exploratory design exercise. It does not run infrastructure, measure capacity or replace the lesson’s quiz.
Apply the pattern to two situations. Select one option in each situation to build a decision record. Every option and its consequence stays visible, and you can change your choices.
When it helps—and what it costs
Use it when the sequence is stable and subclasses have a small set of well-defined steps to customize. Make required preconditions and postconditions visible.
Inheritance ties variants to the base workflow. Unclear hooks can let subclasses violate the sequence’s assumptions. Large base classes with many optional hooks are difficult to understand; composition may expose dependencies more clearly.
Distinguish the neighbouring patterns
Strategy supplies an interchangeable algorithm to a context. Template Method specializes steps in an inherited sequence. Factory Method can be one creation step within that sequence.
Follow algorithm selection through composition, a creation hook within a workflow, ordered processing stages to compare the specific responsibility before combining patterns.
Check your pattern reasoning
Check your pattern reasoning
Name the responsibility before choosing the pattern
For Template Method, describe the problem, the participating roles and the observable contract. Use the example as a starting point and test the boundaries identified in the trade-offs. A pattern name alone does not establish correctness or performance.
Return to the design-pattern guide to compare families, or apply the idea alongside ordered processing stages.
Reaching the recap and completing the quizzes are separate milestones.