The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Presentation on Template Method Design Pattern
-
Upload
asif-tayef -
Category
Education
-
view
403 -
download
0
Transcript of Presentation on Template Method Design Pattern
1
Template Method Design Pattern
Presented by- Ridwan Hossain Talukder [23]
Asif Mahmud [31]
Sanad Saha [17]
2
Outline
• Introduction to Template Pattern.
• Why is it needed?
• A real life scenario.
• How the pattern works.
• Why is it different from other similar design patterns?
What is template pattern?
• The Template Method Pattern defines the skeleton of an algorithm in a method deferring some steps to subclasses.
• The Template Method Pattern lets subclasses redefine certain steps of an algorithm.
• But it doesn’t allow the subclasses to change the algorithm’s structure.
3
Generalized Class Diagram
4
AbstractClass
templateMethod()primitiveOperation1()primitiveOperation2()
ConcreteClass
primitiveOperation1()primitiveOperation2()
primitiveOperation1()primitiveOperation2()
1. The abstract class contains the template
method2. And abstract versions
of the operations used in the template
method
The template method makes use
of the primitiveOperations
to implement an algorithm
There may be many concreteClasses,
each implementing the full set of
operations required by the template
method
Concrete Methods & Hooked methods
• It might be a case that a primitiveOperation is similar for all concreteClasses, then it can be declared as a final method inside the abstractClass.
• Another case might be that a primitiveOperation is not needed in some concreteClasses, then that method can be written inside those concreteClasses with a empty body which are called Hooked Methods
5
Real Life Scenario
• A Software which includes games in which players play against the others, but only one is playing at a given time.
6
CHESSLUD
O
MONOPOLY
The Similarities & The Dissimilarities
Similarities
• All these games have more than one player
• Only a player can play/ make moves at a single time
• Every game has an end
Dissimilarities
• Each game has its own rules, regulations and method of play
• Each game has a different way to choose the winner
7
One Way to do itpublic class Game { public String game; public Game(String game) { this.game = game; } protected int playersCount; void initializeGame() { if(game.equals(“Chess”)) { // Initialize players // Put the pieces on the board } else if(game.equals(“Monopoly”)) { // Initialize players // Initialize money } } void makePlay(int player) { //Similar }
8
One Way to do it
boolean endOfGame(game) { //Similar } void printWinner(game) { //Similar }
public void playOneGame(int playersCount) { this.playersCount = playersCount; initializeGame(); int j = 0; while (!endOfGame()) { makePlay(j); j = (j + 1) % playersCount; } printWinner(); }}
9
What is the problem?
• Every time a new game needs to be added, code have to be modified.
• It violates a basic OOP rule which is -
Open for extension, closed for modification
10
Solution
11
Template Method Design
Pattern!!!
Class Diagram
12
game
<<Template method>>playOneGame()
<<Abstract Methods>>initializeGame()makePlay()endOfGame()
<<Concrete Methods>>printWinner()
chess
initializeGame()makePlay()endOfGame()
initializeGame()makePlay()endOfGame() printWinner()
Monopoly
initializeGame()makePlay()endOfGame()
Implementationabstract class Game { protected int playersCount; abstract void initializeGame(); abstract void makePlay(int player); abstract boolean endOfGame(); /* A template method: */ public final void playOneGame(int playersCount) { this.playersCount = playersCount; initializeGame(); int j = 0; while (!endOfGame()) { makePlay(j); j = (j + 1) % playersCount; } printWinner(); } /* Concrete Methods: */ void printWinner() { // Displays who won }}
13
Implementationclass Monopoly extends Game { /* Concrete implementation of abstract methods For MONOPOLY GAME */ void initializeGame() { // Initialize players // Initialize money } void makePlay(int player) { // Process one turn of player } boolean endOfGame() { // Return true if game is over // according to Monopoly rules }
/* Specific declarations for the Monopoly game. */ // ...}
14
Implementationclass Chess extends Game { /* Concrete implementation of abstract methods For CHESS GAME */ void initializeGame() { // Initialize players // Put the pieces on the board }
void makePlay(int player) { // Process a turn for the player }
boolean endOfGame() { // Return true if in Checkmate or // Stalemate has been reached }
/* Specific declarations for the chess game. */ // ...}
15
Applicability
• To implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.
• When refactoring is performed and common behavior is identified among classes. A abstract base class containing all the common code (in the template method) should be created to avoid code duplication and redundancy.
16
Another Powerful tool: The Hook
• Hook is a method that is declared in the abstract class
• “Hook” lets us to reconfigure our Template
• Hook gives us the choice if we want to override a method or not.
17
Using Hook in Template Pattern
18
abstract class AbstractClass {
final void templateMethod() {
primitiveOperation1();primitiveOperation2();
concreteOperation();
if(HOOK_CustomerWantsOperation3()) { primitiveOperation3(); } }
abstract void primitiveOperation1(); abstract void primitiveOperation2(); abstract void primitiveOperation3();
final void concreteOperation() { } boolean HOOK_CustomerWantsOperation3() { if(condition satisfied) return true;
return false; }}
Difference with Strategy Pattern
• Strategy pattern means choosing an algorithm whereas Template pattern means using an algorithm as a template to do similar but not the same jobs.
• The Strategy pattern allows an algorithm to be chosen at runtime by containment whereas with the Template method pattern this happens at compile-time by subclassing the template.
• The difference consists in the fact that Strategy uses delegation while the Template Methods uses the inheritance.
19
20
THANK YOU!