Template Design Pattern : Template design pattern comes under behavioral design pattern which provides a way to let objects communicate with each other.
In template design pattern , we have template method which defines the steps to execute an algorithm and subclasses will provide the implementation of these steps.
How to implement Template Pattern :
To implement template pattern we need to define a template method in an abstract class .Inside this method we define steps of algorithm which are to be implemented by subclasses .So in this way, template pattern provides a skeleton/framework to the subclasses.
Meal class: (Abstract Class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public abstract class Meal { public final void prepareMeal() { getIngredients(); cookingMeal(); servingMeal(); } public abstract void getIngredients(); public abstract void cookingMeal(); public void servingMeal() { System.out.println("Serving ordered meals to customers"); } } |
VegMeal : (Subclass Implementing abstract methods)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class VegMeal extends Meal { @Override public void getIngredients() { System.out.println("Getting Rice, Vegetable, flour and spices for veg meal preparation"); } @Override public void cookingMeal() { System.out.println("Cooking veg meal"); } } |
NonVegMeal : (Subclass Implementing abstract methods)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class NonVegMeal extends Meal { @Override public void getIngredients() { System.out.println("Getting Rice, chicken , flour and spices for non veg meal preparation"); } @Override public void cookingMeal() { System.out.println("Cooking Non-Veg meal"); } } |
TemplateDemo : (Client Class)
1 2 3 4 5 6 7 8 9 10 11 |
public class TemplateDemo { public static void main(String[] args) { Meal vegmeal = new VegMeal(); vegmeal.prepareMeal(); Meal nonvegmeal = new NonVegMeal(); nonvegmeal.prepareMeal(); } } |
OutPut:
Getting Rice, Vegetable, flour and spices for veg meal preparation
Cooking veg meal
Serving ordered meals to customers
Getting Rice, chicken, flour and spices for non-veg meal preparation
Cooking Non-Veg meal
Serving ordered meals to customers
In above example of the template method pattern, Meal is an abstract class with a template method defined as prepareMeal () which defines the steps (algorithm)involved in meal preparation. We declare template method as final so that it can’t be overridden by subclasses. The steps(algorithm)defined by prepareMeal () consists of three steps:getIngredients(),cookingMeal() and servingMeal().Default implementation of servingMeal () method is already given in the abstract Meal class ,so subclasses can override this implementation as it is. The getIngredients() and cookingMeal() methods are declared abstract so that subclasses need to implement them according to their need.
When to use Template Pattern:Use template pattern where you have business logic that number of application components shared the same processes but the implementation was slightly different.
Some Use Cases:
1)In Java,Servlet class HttpServlet is an abstract class and its doGet, doPost, doPut methods are example of template method pattern usage in Java API.
2) AbstractSet and AbstractMap are example of template design pattern
3)In RealTime scenario,to construct a house architect prepares plan which consists of steps required to build house.