| Template Method Pattern |
Article Index for Template |
Website Links For Template |
Information AboutTemplate Method Pattern |
| CATEGORIES ABOUT TEMPLATE METHOD PATTERN | |
| software design patterns | |
| algorithms | |
| operations research | |
| articles with example java code | |
|
In Software Engineering , the template method pattern is a Design Pattern . A template method defines the skeleton of an Algorithm in terms of abstract operations which subclasses override to provide concrete behavior. First a class is created that provides the basic steps of an algorithm by using abstract methods. Later on, subclasses change the abstract methods to implement real actions. Thus the general algorithm is saved in one place but the concrete steps may be changed by the subclasses. EXAMPLE IN JAVA
abstract class Game{ private int playersCount; abstract void initializeGame(); abstract void makePlay(int player); abstract boolean endOfGame(); abstract void printWinner();
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 extends Game{
void initializeGame(){ // ... } void makePlay(int player){ // ... } boolean endOfGame(){ // ... } void printWinner(){ // ... }
// ... } class Chess extends Game{
void initializeGame(){ // ... } void makePlay(int player){ // ... } boolean endOfGame(){ // ... } void printWinner(){ // ... }
// ... } EXTERNAL LINKS |
|
|