Template Pattern

Template Pattern with Patterns, design, Creational Design, Abstract Factory Pattern, singleton design patterns, Adapter, Visitor Pattern, Template Pattern, Command Pattern, State Pattern, java etc.

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".


Benefits:

  • It is a 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 duplication.

UML for 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.       
  4.        abstract void initialize();  
  5.        abstract void start();  
  6.        abstract void end();  
  7.       
  8.        public final void play(){  
  9.   
  10.           //initialize the game  
  11.           initialize();  
  12.   
  13.           //start game  
  14.           start();  
  15.             
  16.           //end game  
  17.           end();  
  18.        }  
  19. }// 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.   
  3. public class Chess extends Game {  
  4.      @Override  
  5.        void initialize() {  
  6.           System.out.println("Chess Game Initialized! Start playing.");  
  7.        }  
  8.      @Override  
  9.        void start() {  
  10.           System.out.println("Game Started. Welcome to in the chess game!");  
  11.        }  
  12.     @Override  
  13.        void end() {  
  14.           System.out.println("Game Finished!");  
  15.        }  
  16. }// 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.   
  3.   
  4. public class Soccer extends Game {  
  5.       
  6.     @Override  
  7.        void initialize() {  
  8.           System.out.println("Soccer Game Initialized! Start playing.");  
  9.        }  
  10.   
  11.     @Override  
  12.        void start() {  
  13.           System.out.println("Game Started. Welcome to in the Soccer game!");  
  14.        }  
  15.          
  16.     @Override  
  17.        void end() {  
  18.           System.out.println("Game Finished!");  
  19.        }  
  20. }// End of the Soccer class.  

Step 4:

Create a TemplatePatternDemo class.

  1. //This is a class.  
  2. public class TemplatePatternDemo {  
  3.   
  4. public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {  
  5.           
  6.          Class c=Class.forName(args[0]);  
  7.          Game game=(Game) c.newInstance();  
  8.          game.play();     
  9.        }  
  10. }// End of the Soccer class.