0% found this document useful (0 votes)
59 views

Template Method Pattern: Starbuzz Problem

The document describes the template method pattern, which defines the structure of an algorithm while allowing subclasses to implement specific steps. It provides an example of making coffee and tea using the template method pattern to encapsulate the common recipe process and avoid duplicating code across different beverage classes. Subclasses implement unique steps like brewing coffee or steeping tea while retaining the overall recipe structure and logic defined in the abstract superclass.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Template Method Pattern: Starbuzz Problem

The document describes the template method pattern, which defines the structure of an algorithm while allowing subclasses to implement specific steps. It provides an example of making coffee and tea using the template method pattern to encapsulate the common recipe process and avoid duplicating code across different beverage classes. Subclasses implement unique steps like brewing coffee or steeping tea while retaining the overall recipe structure and logic defined in the abstract superclass.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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

By Võ Văn Hải - [email protected] 2

Simple Solution

By Võ Văn Hải - [email protected] 3

1
30/01/2010

Simple Solution

By Võ Văn Hải - [email protected] 4

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.

By Võ Văn Hải - [email protected] 5

More General Approach

By Võ Văn Hải - [email protected] 6

2
30/01/2010

Abstracting Prepare Recipe


Coffee Tea
prepareRecipe(){ prepareRecipe(){
boilWatere(); boilWatere();
brewCoffeeGrinds(); steepTeaBag();
pourInCup(); pourInCup();
addSugarAndMilk(); addLemon();
} }

prepareRecipe(){
boilWatere();
brew();
pourInCup();
addCondiments();
}

By Võ Văn Hải - [email protected] 7

By Võ Văn Hải - [email protected] 8

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.

By Võ Văn Hải - [email protected] 9

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.

By Võ Văn Hải - [email protected] 10

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.

By Võ Văn Hải - [email protected] 11

Template Pattern Diagram

By Võ Văn Hải - [email protected] 12

4
30/01/2010

Code for the Template

By Võ Văn Hải - [email protected] 13

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.

By Võ Văn Hải - [email protected] 14

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.

By Võ Văn Hải - [email protected] 15

5
30/01/2010

Example

By Võ Văn Hải - [email protected] 16

By Võ Văn Hải - [email protected] 17

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.

By Võ Văn Hải - [email protected] 18

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.

By Võ Văn Hải - [email protected] 19

You might also like