Wednesday, September 8, 2010

The Template Method

Reference Site : http://en.wikipedia.org/wiki/Template_method_pattern
Reference Book:Gang of Four - Design Patterns, Elements of Reusable Object Oriented Software

Topic Name:Template Method (Design Pattern )
Introduction :
1-The template method pattern is a design pattern.

2-Template Method is part of the Behavioral Patterns(prescribe the way objects interact with each other).

 3-Template Method is used to represent Parent and child relation ship,it force to subclass to implement the abstract method and also it's provided the blue Print or out line to the subclass.

Description of the problem:
I have a AbstractGame.This class is represent the Game Play between Two team .it have getWinner() method .which is give the winner between  two team on the basic of the score of individual .Let me give the proto type of getWinner()

Proto Type:public Team getWinner();
We have two  other type of the  class and they are  extends the AbstractGame class.Classes are following
1-StratingGame  it represent the starting game(Direct show the relation ship between two team )
2-IntermediateGame it represent the intermediate game or winner of previously game
On the basic of individual Characterised both class implement the getWinner() algorithm differently way .
getWinner() algorithm is the same for both types of games but the algorithm for determining team1 and team2 is different: static for starting games and the winner's of the previous games for intermediate games.
Solution Of this Problem: Using Template we are solve this problem

                                                  TM-pattern-problem

TM-pattern-solution
Example:
Supper Class is AbstractGame
-------------------------------------------------------------------
public abstract AbstractGame {
protected Integer team1Score;
protected Integer team2Score;
//abstract method
public abstract String getTeam1();
public abstract String getTeam2();
//"blueprint"
//Template Method
public String getWinner(){
if(team1Score>team2Score){
return getTeam1();
}
else {
return getTeam2();
}
}
---------------------------------------------------------------------
 StratingGame
---------------------------------------------------------------------
public StratingGame extends AbstractGame {
private String team1;
private String team2;
public String getTeam1(){
return team1;
}
public String getTeam2(){
return team2;
}
}
---------------------------------------------------------------------
Intermediate Game
--------------------------------------------------------------------
public class IntermediateGame extends AbstractGame {
private AbstractGame game1;

private AbstractGame game2;
//
// Accessor methods
//
public AbstractGame getGame1() {
return game1;
}
public void setGame1(AbstractGame game1) {
this.game1 = game1;
}
public AbstractGame getGame2() {
return game2;
}
public void setGame2(AbstractGame game2) {
this.game2 = game2;
}
@Override
public Team getTeam1(){
return getGame1().getWinner();
}
@Override
public Team getTeam2() {
return getGame2().getWinner();
}
}
-----------------------------------------------------------------------------------------------------------