Template Method Pattern
Template Method Pattern
Template Method Pattern
Pattern
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 }
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
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
Need abstraction?
Act-1: Encapsulate
No What Varies, methods No
and its corresponding
attributes Need composition?
Yes Yes
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
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
doCreateDocument()
canOpenDocument()
readDocument()
openDocument()
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
14
Requirements Statement2
Please follow these recipes precisely when
preparing Starbuzz beverages
Starbuzz Tea Recipe
• Boil some water Tea
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();
} }
17
Design Process for Change
Need abstraction?
Act-1: Encapsulate
No What Varies, methods No
and its corresponding
attributes Need composition?
Yes Yes
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
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