In this post, We will talk and learn about the Template Design Pattern in Java.
Key Points About Template Design Pattern :
- Template Design Pattern is a behavioral design pattern. Template Method design pattern is used to create a method stub and that differs some of the steps of implementation to the subclasses.
- Template Design Pattern defines the Execution Order to execute an algorithm and it can provide a default implementation that might be common for all or some of the subclasses.
- The template method should consist of certain steps of an algorithm whose order is fixed and for some of the methods, an implementation might differ from base class to subclass. If we want some of the method should not be overridden by subclasses then we can make those template methods final
Template Design Pattern in JDK & Framework:
- Most of the non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap.
- servlet.http.HttpServlet all of the action methods ( doXXX()) methods by default sends an HTTP 405 “Method Not Allowed” error to the response. So you’re free to implement none or any of them.
- Below are the few of Sprig framework classes which uses Method Template design pattern
- JdbcTemplate, HibernateTemplate, JmsTemplate, RestTemplate, SimpleJdbcTemplate,TransactionTemplate
Now Let’s move towards the implementation of the Template Design Pattern.
Below is the complete source code:
PizzaTemplate.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.kkjavatutorials.pizza; /** * @author KK JavaTutorials *This is a base template class */ public abstract class PizzaTemplate { //Sub class can not override this template method public final void preparePizza() { /* Methods Oder of Execution is Fixed in Template Method*/ selectBread(); addingIngredients(); cooking(); addingCheese(); addingTopinngs(); } /*Implementation of selectBread & addingIngredients Sub Class has to provide*/ public abstract void selectBread(); public abstract void addingIngredients(); //Optionally can be overridden by Sub Class public void cooking() { System.out.println("Cooking Pizza for 15 minutes!"); } //Optionally can be overridden by Sub Class public void addingTopinngs() { System.out.println("Adding Topinngs in Pizza"); } //Optionally can be overridden by Sub Class public final void addingCheese() { System.out.println("Adding Cheese in Pizza"); } } |
VegPizza.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.kkjavatutorials.pizza; /** * @author KK JavaTutorials * This class is the one of implementation of Template * Responsible to cook Veg Pizza */ public class VegPizza extends PizzaTemplate { @Override public void selectBread() { System.out.println("Choosing Bread for Veg-Pizza!!"); } @Override public void addingIngredients() { System.out.println("Adding ingredients in Veg-Pizza!!"); } } |
NonVegPizza.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.kkjavatutorials.pizza; /** * @author KK JavaTutorials *This class is the one of implementation of Template *Responsible to cook NonVeg Pizza */ public class NonVegPizza extends PizzaTemplate { @Override public void selectBread() { System.out.println("Choosing Bread for NonVeg-Pizza!!"); } @Override public void addingIngredients() { System.out.println("Adding ingredients in NonVeg-Pizza!!"); } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.kkjavatutorials.client; import com.kkjavatutorials.pizza.NonVegPizza; import com.kkjavatutorials.pizza.PizzaTemplate; import com.kkjavatutorials.pizza.VegPizza; /** * @author KK JavaTutorials *This is Client program for Template design Pattern */ public class ClientTest { public static void main(String[] args) { PizzaTemplate pizzaTemplate = new VegPizza(); pizzaTemplate.preparePizza(); System.out.println("----------------------------------"); pizzaTemplate = new NonVegPizza(); pizzaTemplate.preparePizza(); } } |
Output of this program:
Choosing Bread for Veg-Pizza!!
Adding ingredients in Veg-Pizza!!
Cooking Pizza for 15 minutes!
Adding Cheese in Pizza
Adding Topinngs in Pizza
———————————-
Choosing Bread for NonVeg-Pizza!!
Adding ingredients in NonVeg-Pizza!!
Cooking Pizza for 15 minutes!
Adding Cheese in Pizza
Adding Topinngs in Pizza
You May Also Like:
Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java
Adapter Design Pattern in Java
Decorator Design Pattern in Java
Facade Design Pattern in Java
Bridge Design Pattern in Java.
That’s all about the Template Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.