In Template design pattern, we define the steps to execute an algorithm in a template method of an abstract base class without implementing some steps of the algorithm. It may contain the default implementation of the steps of algorithm that might be common for all or some of the subclasses. Its subclasses can override the specific steps of the algorithm as per requirement of subclasses but the algorithm execution sequence will be in the same way as defined by an abstract base class.
Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. In simple words, the template methods of the abstract base class declares algorithm with abstract placeholder methods and derived classes implement the placeholder methods. Template pattern comes under behavior design pattern category.
To preparing a pizza we need to execute following three steps in sequence
- Step 1. Prepare pizza base.
- Step 2. Add toppings on pizza.
- Step 3. Bake pizza in oven.
In this example, template method of base class "Pizza" will contain the sequential steps of pizza prepatation where as subclasses for "veg pizza" and "chicken pizza" pizza type will contain specific implementations of second step.
Implementation of Template Design Pattern
We will create an abstract class called Pizza containing a template method preparePizza which define the generic sequence of steps to prepare a pizza. Each step of pizza preparation process is defined as an abstract method which acts as place holder. Each subclass(VegPizza and ChickenPizza) will have specific implemetation of each step for pizza preparation as required for that pizza type.
Abstract class Pizza contains template method preparePizza which defines the algorithn to prepare pizza as abstract methods.
public abstract class Pizza { abstract void preparePizzaBase(); abstract void addToppings(); abstract void bakePizza(); //Template method public final void preparePizza(){ // Prepare Pizza Base preparePizzaBase(); // Add Toppings addToppings(); // Bake Pizza bakePizza(); } }
VegPizza.java
VegPizza is a concrete class extending abstract class Pizza. It implements abstract methods as per the requirement to prepare a veg pizza.
public class VegPizza extends Pizza { @Override void preparePizzaBase() { System.out.println("Prepairing pizza base for Veg Pizza"); } @Override void addToppings() { System.out.println("Adding toppings for Veg Pizza"); } @Override void bakePizza() { System.out.println("Baking Veg Pizza"); } }
ChickenPizza.java
ChickenPizza is a concrete class extending abstract class Pizza. It implements abstract methods as per the requirement to prepare a chicken pizza.
public class ChickenPizza extends Pizza { @Override void preparePizzaBase() { System.out.println("Prepairing pizza base for Chicken Pizza"); } @Override void addToppings() { System.out.println("Adding toppings for Chicken Pizza"); } @Override void bakePizza() { System.out.println("Baking Chicken Pizza"); } }
TemplatePatternDemo.java
TemplatePatternDemo class will create an instance of VegPizza and ChickenPizza and call preparePizza method of each pizza type. For the output, we can verify that the sequence fo steps to prepare a pizza remains same for both pizza type but implementation of each step is different for each pizza type.
public class TemplatePatternDemo { public static void main(String[] args) { Pizza pizza = new VegPizza(); pizza.preparePizza(); System.out.println(); pizza = new ChickenPizza(); pizza.preparePizza(); } }
Output
Prepairing pizza base for Veg Pizza Adding toppings for Veg Pizza Baking Veg Pizza Prepairing pizza base for Chicken Pizza Adding toppings for Chicken Pizza Baking Chicken Pizza
Related Topics