Template Pattern

Template Pattern

A Template Pattern says that "just define the skeleton of a function in an operation, deferring some steps to its subclasses". 

Or, Template method design pattern is to define an algorithm as skeleton of operations and leave the details to be implemented by the child classes. The overall structure and sequence of the algorithm is preserved by the parent class.
Template means Preset format like HTML templates which has fixed preset format.Similarly in template method pattern,we have a preset structure method called template method which consists of steps.This steps can be abstract method which will be implemented by its subclasses.

Benefits:

  • It is very common technique for reusing the code.This is only the main benefit of it.

Usage:

  • It is used when the common behavior among sub-classes should be moved to a single common class by avoiding the duplication.

UML for Template Pattern:

Template Pattern

Implementation of Template Pattern:

Step 1:
Create a Game abstract class.
  1. //This is an abstract class.  
  2. public abstract class Game {  
  3.        abstract void initialize();  
  4.        abstract void start();  
  5.        abstract void end();  
  6.        public final void play(){    
  7.           //initialize the game  
  8.           initialize();  
  9.           //start game  
  10.           start(); 
  11.           //end game  
  12.           end();  
  13.        }  
  14. }// End of the Game abstract class.  
Step 2:
Create a Chess class that will extend Game abstract class for giving the definition to its method.
  1. //This is a class.   
  2. public class Chess extends Game {  
  3.      @Override  
  4.        void initialize() {  
  5.           System.out.println("Chess Game Initialized! Start playing.");  
  6.        }  
  7.      @Override  
  8.        void start() {  
  9.           System.out.println("Game Started. Welcome to in the chess game!");  
  10.        }  
  11.     @Override  
  12.        void end() {  
  13.           System.out.println("Game Finished!");  
  14.        }  
  15. }// End of the Chess class.  
Step 3:
Create a Soccer class that will extend Game abstract class for giving the definition to its method.
  1. //This is a class.  
  2. public class Soccer extends Game {  
  3.     @Override  
  4.        void initialize() {  
  5.           System.out.println("Soccer Game Initialized! Start playing.");  
  6.        }  
  7.     @Override  
  8.        void start() {  
  9.           System.out.println("Game Started. Welcome to in the Soccer game!");  
  10.        }  
  11.     @Override  
  12.        void end() {  
  13.           System.out.println("Game Finished!");  
  14.        }  
  15. }// End of the Soccer class.  
Step 4:
Create a TemplatePatternDemo class.
  1. //This is a class.  
  2. public class TemplatePatternDemo {  
  3. public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {  
  4.          Class c=Class.forName(args[0]);  
  5.          Game game=(Game) c.newInstance();  
  6.          game.play();     
  7.        }  
  8. }// End of the Soccer class.  
Output:
Template Pattern

Comments

Popular posts from this blog

gsutil Vs Storage Transfer Service Vs Transfer Appliance

SQL basic interview question