30/01/2010
TEMPLATE METHOD
PATTERN
By Võ Văn Hải
Email: [email protected]
Blog: http://vovanhai.wordpress.com
Saigon, 01/2010
StarBuzz problem
• Coffee Recipe
– Boil some water
– Brew coffee in boiling water
– Pour coffee in cup
– Add sugar and milk
• Tea Recipe
– Boil some water
– Steep tea in boiling water
– Pour tea in cup
– Add lemon
• Suppose you are required to implement a system
to maintain this
Simple Solution
1
30/01/2010
Simple Solution
Problems with the Solution
Code is duplicated across the classes –
code changes would have to be made in
more than one place.
Adding a new beverage would result in
further duplication.
Knowledge of the algorithm and
implementation is distributed over classes.
More General Approach
2
30/01/2010
Abstracting Prepare Recipe
Coffee Tea
prepareRecipe(){ prepareRecipe(){
boilWatere(); boilWatere();
brewCoffeeGrinds(); steepTeaBag();
pourInCup(); pourInCup();
addSugarAndMilk(); addLemon();
} }
prepareRecipe(){
boilWatere();
brew();
pourInCup();
addCondiments();
}
Advantages of the New Approach
A single class protects and controls the
algorithm, namely, CaffeineBeverage.
The superclass facilitates reuse of methods.
Code changes will occur in only one place.
Other beverages can be easily added.
3
30/01/2010
This is the Template Pattern
The prepareRecipe() method implements the
template pattern.
This method serves as a template for an algorithm,
namely that for making a caffeinated beverage.
In the template each step is represented by a
method.
Some methods are implemented in the superclass.
Other method must be implemented by the
subclass and are declared abstract.
The template pattern defines the steps of an
algorithm and allows the subclasses to implement
one or more of the steps.
Template Pattern
Encapsulates an algorithm by creating a
template for it.
Defines the skeleton of an algorithm as a set
of steps.
Some methods of the algorithm have to be
implemented by the subclasses – these are
abstract methods in the super class.
The subclasses can redefine certain steps of
the algorithm without changing the algorithm’s
structure.
Some steps of the algorithm are concrete
methods defined in the super class.
Template Pattern Diagram
4
30/01/2010
Code for the Template
Hooked on Template Method
A hook is a method that is declared in the
abstract class, but only given an empty or
default implementation.
This gives subclasses the ability to “hook
into” the algorithm at various points, if they
wish; a subclasses is also free to ignore the
hook.
Why Hooks
The number of abstract methods used must
be minimized.
Enables a subclass to implement an optional
part of an algorithm.
Enables a subclass to react to a step in the
template method.
Enables the subclass to make a decision for
the abstract class.
5
30/01/2010
Example
Hollywood Principle
The template pattern follows the
hollywood principle.
Principle: Don’t call us, we will call you.
Low-level components are activated by
high-level components.
A low-level component never calls a high-
level component.
In the template pattern the abstract class is
the high-level component and the concrete
classes the low-level components.
6
30/01/2010
Summary
Design Principle: Don’t call us we’ll call you.
Template pattern defines steps of an
algorithm.
Subclasses cannot change the algorithm -
final
Facilitates code reuse.
Similar to the strategy pattern.
The factory pattern is a specialization of
the template pattern.