Define the skeleton of an algorithm and let subclasses override specific steps. You want consistent flow with variable steps. You need shared
Template Method
Overview
Define the skeleton of an algorithm and let subclasses override specific steps.
When to use
- You want consistent flow with variable steps.
- You need shared algorithm structure.
Java example
TypeScript example
Pros and cons
Pros:
- Enforces consistent algorithm structure.
- Reduces duplicated code.
Cons:
- Inheritance can reduce flexibility.
- Hard to change the base flow.
Common pitfalls
- Overusing inheritance for small variations.
- Adding too many hook methods.