Thursday, September 22, 2011

template method pattern

template pattern provide abstract definition of methods in a class and,redefine it's behavior later on the fly and without change its structures,at same time it will keep overall execute step sequence.

abstract class Game{
void init();
void play();
void printWinner();
void exit();
void playTheGame(){
init();
play();
printWinner();
exit();
}
}

class MonoGame extends Game(){
void init(){
System.out.println("init monoGame");
}
void play(){
System.out.println("playing monoGame");
}
void printWinner(){
System.out.println("print winner in monoGame");
}
void exit(){
System.out.println("exit monoGame");
}
}
//define other classes with new behaviors with override the abstract method
public static void main(String args[]){
Game g = new MonoGame();
g.playTheGame();
//other type of games
}

No comments:

Post a Comment