Design Patterns · Middle · Question 13 of 75
How does Template Method differ from Strategy?
- ✓Correct answer: Template Method varies steps via inheritance, Strategy via composition
- 2Strategy varies steps via inheritance, while Template Method uses composition
- 3Template Method is chosen at runtime, Strategy at compile time
Explanation
Template Method fixes the algorithm's skeleton in a base class, and the variation is set by a subclass (statically). A strategy is passed to an object and can change at runtime. In JS, composition is usually preferred: pass step functions.