What is the Template Method Pattern?
- Defines the skeleton of an algorithm in a method
- Defers some steps to a subclass
- Lets subclasses change some steps in the algorithm without changing the structure
- The Template Method contains an ordered series of primitive operations -- by (re)defining the primitive operations, subclasses change the algorithm
What Are Hooks?
- A Hook is a method defined in the base class with a default or empty operation
- Because it's not abstract, subclasses can ignore it
- Because it's there, subclasses can re-define it without changing the structure of the template
- If the hook returns a boolean for a conditional, it allows subclasses to change the flow of the algorithm
Abstract Vs Hooks
- Abstract Methods are requirements
- Hooks are optional
What Is the 'Hollywood Principle'?
- Don't call us, we'll call you.
- High-level components call low-level ones, not the other way around
Strategy Vs Template
- Strategy composes other objects chosen at runtime
- Template uses sub-classes
- Template provides better code re-use and strictly defines the algorithm
- Strategy provides better decoupling but the algorithm is provided by the composed objects