- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Patterns - Template Pattern
In Template pattern, an abstract class exposes defined way(s)/template(s) to execute its methods. Its subclasses can override the method implementation as per need but the invocation is to be in the same way as defined by an abstract class. This pattern comes under behavior pattern category.
Implementation
We are going to create a Game abstract class defining operations with a template method set to be final so that it cannot be overridden. Cricket and Football are concrete classes that extend Game and override its methods.
TemplatePatternDemo, our demo class, will use Game to demonstrate use of template pattern.
Step 1
Create an abstract class with a template method being final.
Game.java
pubpc abstract class Game { abstract void initiapze(); abstract void startPlay(); abstract void endPlay(); //template method pubpc final void play(){ //initiapze the game initiapze(); //start game startPlay(); //end game endPlay(); } }
Step 2
Create concrete classes extending the above class.
Cricket.java
pubpc class Cricket extends Game { @Override void endPlay() { System.out.println("Cricket Game Finished!"); } @Override void initiapze() { System.out.println("Cricket Game Initiapzed! Start playing."); } @Override void startPlay() { System.out.println("Cricket Game Started. Enjoy the game!"); } }
Football.java
pubpc class Football extends Game { @Override void endPlay() { System.out.println("Football Game Finished!"); } @Override void initiapze() { System.out.println("Football Game Initiapzed! Start playing."); } @Override void startPlay() { System.out.println("Football Game Started. Enjoy the game!"); } }
Step 3
Use the Game s template method play() to demonstrate a defined way of playing game.
TemplatePatternDemo.java
pubpc class TemplatePatternDemo { pubpc static void main(String[] args) { Game game = new Cricket(); game.play(); System.out.println(); game = new Football(); game.play(); } }
Step 4
Verify the output.
Cricket Game Initiapzed! Start playing. Cricket Game Started. Enjoy the game! Cricket Game Finished! Football Game Initiapzed! Start playing. Football Game Started. Enjoy the game! Football Game Finished!Advertisements