The Template Method Pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
Bates and Sierra — Head First Design Patterns
The abstract class contains the template method and abstract versions of the operations used in the template method. Each method, abstract or concrete, is a step of an algorithm which could varies.
The template methods use the
primitiveOperations to implement an algorithm decoupling from the actual implementation of these operations.
The concrete class implements all the abstract operations called by the template method.
##Design Principle The Hollywood Principle.
##Scenario An algorithm is made of steps useful to accomplish some tasks. It can be imagined as a recipe which is a set of instruction to prepare a dish. Some recipes could have some instructions in common so it should be better to avoid code duplication.
- Template method. It defines the skeleton of an algorithm deferring some steps to subclasses. It lets subclasses redefine some steps without changing the algorithm’s structure.
- Abstract class._
- It is a template of methods for an algorithm which could implemented in slightly different ways in some of the steps.
- It is made to be extended and abstract methods to be implemented. So the abstract class collect all the all the common methods or instructions different algorithms.
- It reduces the dependencies in the system.
- Abstract methods. They point out that they are just placeholders because they are in common with all the algorithms but their implementation differ from algorithm to algorithm.
- Concrete methods. Concrete meaning that their implementation is the same and in common among algorithms.
- Inheritance. It allows all the subclasses, the algorithms, to have the same behaviors of the superclass, if they are abstract they could change across subclasses.
- Interfaces. They don’t have code so no code reuse.
- Concrete class. Concrete implementation is a working algorithm which implements each abstract method and could add some other algorithm specific methods.
- Hook methods.
- They are concrete methods doing nothing by default, they are optional steps of the algorithm and the subclasses are not obliged to override them. The subclass can hook its own code into the algorithm, an optional part of the algorithm.
- They could also be used to conditionally control, using conditional statements, the flow of the algorithm in the abstract class.
##Template method vs. Strategy
- Focus on
- Strategy and Template both encapsulate algorithms, one by inheritance and one by composition.
- Define the outline of an algorithm and let my subclasses do some of the work.
- Keep the control over the algorithm’s structure and allow be different implementations of individual steps.
- Provide method for code reuse allowing subclasses to specify behavior.
- Depend on method implemented in the superclass.
- Define a family of algorithms and make them interchangeable.
- Each algorithm is encapsulated so the client can use different algorithms easily.
- Not use inheritance for algorithm implementations.
- Clients use algorithm implementation through object composition.
- Clients can change algorithm at runtime by using different strategy object.
- Not depend on any superclass.
##Template Method in Action