Template Pattern
Source:: Software Design Patterns - Best Practices for Software Developers
Definition
The pattern is defined as allowing subclasses to define parts of an algorithm without modifying the overall structure of the algorithm.
- [i] Factory Method Pattern is a specialization of the template pattern.
- [i] A template method defines an algorithm in terms of abstract operations that subclasses override to provide concrete behavior. The ordering of the steps is fixed by the abstract class.
- [i] Template method should consists of certain steps whose order is fixed.
- [i] Most of the times, subclasses calls methods from super class but in template pattern, superclass template method calls methods from subclasses, this is known as Hollywood Principle - “don’t call us, we’ll call you.”.
- [i] Methods in base class with default implementation are referred to as Hooks and they are intended to be overridden by subclasses.
- A hook denotes an optional step for the subclass to override whereas a method marked as abstract forces the subclasses to provide an implementation for the step.
- The template pattern method is very suitable for frameworks.
- [u] The higher level components don’t depend on lower level components and call the lower level components as and when required.