Template Method
Pattern
Shin-Jie Lee (李信杰)
Associate Professor
Computer and Network Center
Department of CSIE
National Cheng Kung University
Design Aspect of Template
Method
Steps of an algorithm
2
Outline
Requirements Statement
Initial Design and Its Problems
Design Process
Refactored Design after Design Process
Another Example
Recurrent Problems
Intent
Template Method Pattern Structure
3
Homework
Opening Documents
in Applications
(Template)
Shin-Jie Lee (李信杰)
Assistant Professor
Computer and Network Center
Department of CSIE
National Cheng Kung University
Requirements Statement1
In order to open a text document, a text
application will:
Check if the text document can be opened
Create a text document object and hold a reference
of the text document object.
Add the text document object to the Application.
Read text document.
if (!canOpenTextDocument(name)) {
// cannot handle this document
TextApplication return;
}
addDocument()
doCreateTextDocument() Document doc = doCreateTextDocument();
canOpenTextDocument()
readTextDocument() if (doc != null) {
openDocument() addDocument(doc);
readTextDocument(doc);
}
5
Requirements Statement2
There is another new requirement.
Opening a spreadsheet document with a spreadsheet application carries
the same steps in the algorithm (process) as the text document.
Check if the spreadsheet document can be opened.
Create a spreadsheet document object and hold a reference of the
spreadsheet document object.
Add the spreadsheet document object to the Application.
Read spreadsheet document.
if (!canOpenSpreadsheetDocument(name)) {
// cannot handle this document
return;
SpreadsheetApplication }
Document doc = DoCreateSpreadsheetDocument();
addDocument()
doSpreadsheetCreateSpreadsheetDocument() if (doc != null) {
canOpenSpreadsheetDocument() addDocument(doc);
readSpreadsheetDocument() readSpreadsheetDocument(doc);
openDocument() }
6
Initial Design
SpreadsheetApplication TextApplication
addDocument() addDocument()
doSpreadsheetCreateSpreadsheetDocument() doCreateTextDocument()
canOpenSpreadsheetDocument() canOpenTextDocument()
readSpreadsheetDocument() readTextDocument()
openDocument() openDocument()
if (!canOpenTextDocument(name)) {
if (!canOpenSpreadsheetDocument(name)) {
// cannot handle this document
// cannot handle this document
return;
return;
}
}
Document doc = DoCreateTextDocument();
Document doc = DoCreateSpreadsheetDocument();
if (doc != null) {
if (doc != null) {
addDocument(doc);
addDocument(doc);
readTextDocument(doc);
readSpreadsheetDocument(doc);
}
}
7
Problem with the Initial Design
Problem: Both the SpreadsheetApplication and
TextApplication classes will be modified if the
algorithm (duplicate code) of opening
spreadsheet and text is changed.
SpreadsheetApplication TextApplication
addDocument() addDocument()
doSpreadsheetCreateSpreadsheetDocument() doCreateTextDocument()
canOpenSpreadsheetDocument() canOpenTextDocument()
readSpreadsheetDocument() readTextDocument()
openDocument() openDocument()
if (!canOpenTextDocument(name)) {
if (!canOpenSpreadsheetDocument(name)) {
// cannot handle this document
// cannot handle this document
return;
return;
}
}
Document doc = DoCreateTextDocument();
Document doc = DoCreateSpreadsheetDocument();
if (doc != null) {
if (doc != null) {
addDocument(doc);
addDocument(doc);
readTextDocument(doc);
readSpreadsheetDocument(doc);
}
}
8
Design Process for Change
Design Principle: Encapsulate what
The code that changes has been varies.
encapsulated as a class?
Need abstraction?
Act-1: Encapsulate
No What Varies, methods No
and its corresponding
attributes Need composition?
Yes Yes
Act-2: Abstract Common Behaviors No
(with a same signature) into Interfaces
or Abstract Classes Yes
Design Principle: Program to an
interface, not an implementation.
Act-3: Compose or
Delegate Abstract Expose new interfaces?
Design Principle: Depend on
abstractions. Do not depend on Behaviors
concrete classes.
Yes No
9
Act-1: Encapsulate What Varies
Act-1.2: Encapsulate a method (an
interchangeable behavior) into a
concrete class
SpreadsheetApplication TextApplication
addDocument() addDocument()
doSpreadsheetCreateSpreadsheetDocument() doCreateTextDocument()
canOpenSpreadsheetDocument() canOpenTextDocument()
readSpreadsheetDocument() readTextDocument() Application
openDocument() openDocument()
openDocument()
if (!canOpenTextDocument(name)) {
if (!canOpenSpreadsheetDocument(name)) {
// cannot handle this document
// cannot handle this document
return;
return;
}
}
Document doc = DoCreateTextDocument();
Document doc = DoCreateSpreadsheetDocument();
if (doc != null) {
if (doc != null) {
addDocument(doc);
addDocument(doc);
readTextDocument(doc);
readSpreadsheetDocument(doc);
}
}
10
Act-2: Abstract Common Behaviors
Act-2.3: Abstract common
behaviors with a same Application
method body into abstract
class through inheritance addDocument()
doCreateDocument()
canOpenDocument()
readDocument()
openDocument()
Act-2.2: Abstract common
behaviors with a same
signature into abstract class
through inheritance SpreadsheetApplication TextApplication
doCreateDocument() doCreateDocument()
canOpenDocument() canOpenDocument()
readDocument() readDocument()
11
Final Design
if (!canOpenDocument(name)) {
// cannot handle this document
Application return;
}
addDocument()
doCreateDocument() Document doc = DoCreateDocument();
canOpenDocument()
readDocument() if (doc != null) {
addDocument(doc);
openDocument()
readDocument(doc);
}
SpreadsheetApplication TextApplication
doCreateDocument() doCreateDocument()
canOpenDocument() canOpenDocument()
readDocument() readDocument()
12
Prepare Caffeine
Beverages
(Template)
Shin-Jie Lee (李信杰)
Assistant Professor
Computer and Network Center
Department of CSIE
National Cheng Kung University
Requirements Statement1
Please follow these recipes precisely when
preparing Starbuzz beverages
Starbuzz Coffee Recipe
• Boil some water Coffee
• Brew coffee in boiling water prepareRecipe()
boilWater()
• Pour Coffee in cup {
brewCoffeeGrinds()
pourInCup()
boilWater(); addSugarAndMilk()
• Add sugar and milk brewCoffeeGrinds();
pourInCup();
addSugarAndMilk();
}
14
Requirements Statement2
Please follow these recipes precisely when
preparing Starbuzz beverages
Starbuzz Tea Recipe
• Boil some water Tea
• Steep tea in boiling waterprepareRecipe()
boilWater()
• Pour tea in cup steepTeaBag()
addLemon() {
pourInCup() boilWater();
• Add lemon brewTeaBag();
pourInCup();
addLemon();
}
15
Initial Design - Class Diagram
Coffee Tea
prepareRecipe() prepareRecipe()
boilWater() boilWater()
brewCoffeeGrinds() steepTeaBag()
{ pourInCup() addLemon() {
boilWater(); addSugarAndMilk() pourInCup() boilWater();
brewCoffeeGrinds(); brewTeaBag();
pourInCup(); pourInCup();
addSugarAndMilk(); addLemon();
} }
16
Problems with Initial Design
Coffee Tea
prepareRecipe() prepareRecipe()
boilWater() boilWater()
brewCoffeeGrinds() steepTeaBag()
{ pourInCup() addLemon() {
boilWater(); addSugarAndMilk() pourInCup() boilWater();
brewCoffeeGrinds(); brewTeaBag();
pourInCup(); pourInCup();
addSugarAndMilk(); addLemon();
} }
Problem: Both the Coffee and Tea classes will be
modified if the algorithm (duplicate code) of
preparing coffee and tea is changed.
17
Design Process for Change
Design Principle: Encapsulate what
The code that changes has been varies.
encapsulated as a class?
Need abstraction?
Act-1: Encapsulate
No What Varies, methods No
and its corresponding
attributes Need composition?
Yes Yes
Act-2: Abstract Common Behaviors No
(with a same signature) into Interfaces
or Abstract Classes Yes
Design Principle: Program to an
interface, not an implementation.
Act-3: Compose or
Delegate Abstract Expose new interfaces?
Design Principle: Depend on
abstractions. Do not depend on Behaviors
concrete classes.
Yes No
18
Act-1: Encapsulate What Varies
Act-1.2:
Coffee Encapsulate a
prepareRecipe() method (an
boilWater()
brewCoffeeGrinds()
{
interchangeable
pourInCup()
addSugarAndMilk()
boilWater();
brewCoffeeGrinds();
behavior) into a
pourInCup();
addSugarAndMilk();
concrete class CaffeineBeverage
} prepareRecipe()
Tea
prepareRecipe()
boilWater()
steepTeaBag()
pourInCup() {
addLemon() boilWater();
brewTeaBag();
pourInCup();
addLemon();
}
19
Act-2: Abstract Common Behaviors
CaffeineBeverage
Act-2.3: Abstract common
prepareRecipe()
behaviors with a same
boilWater()
method body into abstract
pourInCup() class through inheritance
brew()
{ addCondiments()
boilWater();
brew ();
pourInCup();
Act-2.2: Abstract
}
addCondiment(); common
behaviors with a
same signature
Coffee Tea
into abstract
brew()
addCondiments()
brew()
addCondiments()
class through
inheritance
20
Refactored Design after Design Process
CaffeineBeverage
prepareRecipe()
boilWater()
brew()
{ pourInCup()
boilWater(); addCondiments()
brew ();
pourInCup();
addCondiment();
}
Coffee Tea
brew() brew()
addCondiments() addCondiments()
21
Recurrent Problem
Two classes with code duplications would be
modified at the same time if the duplicate code is
being changed.
22
Intent
Define 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.
23
Template Pattern Structure1
AbstractClass
Client templateMethod() {
primitiveOperation1() …
primitiveOperation2() primitiveOperation1()
primitiveOperation2()
…
}
ConcreteClass
primitiveOperation1()
primitiveOperation2()
24
Template Pattern Structure2
Client ConcreteClass
1: templateMethod()
2: primitiveOperation1()
3: primitiveOperation2()
25
Template Pattern Structure3
Instantiation Use Termination
Abstract Client class uses this abstract
Class Don’t Care class to execute steps of an Don’t Care
algorithm through polymorphism
Concrete Client class uses this class to
Class Client execute steps of an algorithm Don’t Care
through abstract class
26
Homework1: Requirements
Statement
Cut, Copy, Paste on a Document
An editor application carries a document.
A menu in the editor application contains some menu
items which performs three specific operations such as
cut, copy, and paste on a document.
27
Homework2: Requirements
Statement
Remote Control
The remote control can control a stereo remotely.
While a stereo is switched on by the remote control, the
CD and volume will be set at the same time.
28