Google
 
   
Login
Username:

Password:


Lost Password?

Register now!
Search
Main Menu
top books
Polls
What do you think about php-deluxe.net?
Excellent!
Cool
Hmm..not bad
What the hell is this?
encyclopedia
recommendation
compare webbrowser
Freenet DSL
Who's Online
4 user(s) are online (3 user(s) are browsing encyclopedia)

Members: 0
Guests: 4

more...
browser tip
Unix Befehle
manual of unix befehle
recommendation!
Sponsored
partner

Template method pattern

In software engineering, the template method pattern is a software design pattern.

A template method defines the skeleton of an Algorithm in terms of abstract operations which subclasses override to provide concrete behavior.

= Example in Java =

/** * An abstract class that is common to several games in * which players play against the others, but only one is * playing at a given time. */ abstract class Game{ private int playersCount; abstract void initializeGame(); abstract void makePlay(int player); abstract boolean endOfGame(); abstract void printWinner(); /* A template method : */ final void playOneGame(int playersCount){ this.playersCount = playersCount; initializeGame(); int j = 0; while( ! endOfGame() ){ makePlay( j ); j = (j + 1) % playersCount; } printWinner(); } }

Now we can extend this class in order to implement existing games:

class Monopoly (game) extends Game{ /* Implementation of necessary concrete methods */ void initializeGame(){ // ... } void makePlay(int player){ // ... } boolean endOfGame(){ // ... } void printWinner(){ // ... } /* Specific declarations for the Monopoly (game) game. */ // ... }

class Chess extends Game{ /* Implementation of necessary concrete methods */ void initializeGame(){ // ... } void makePlay(int player){ // ... } boolean endOfGame(){ // ... } void printWinner(){ // ... } /* Specific declarations for the Chess game. */ // ... }