design patterns
Template Method
The Template Method pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. The key participants are the AbstractClass (defines abstract primitive operations that concrete subclasses implement, and implements a template method defining the skeleton of the algorithm) and ConcreteClass (implements the primitive operations to carry out subclass-specific steps). Think of building a house: the blueprint (template) defines the sequence — lay foundation, build walls, add roof, install interior — but the specific materials and methods for each step vary by house type. Use Template Method when you want to implement the invariant parts of an algorithm once and leave it to subclasses to implement the behavior that can vary.
Use Cases
- •Data processing pipelines with varying parse/transform steps
- •Game AI with fixed game loop but customizable behaviors
- •Report generators with common structure but different formats
- •Testing frameworks with setup/execute/teardown lifecycle