Here is an example of the template method pattern. Meal is an abstract class with a template method called doMeal() that defines the steps involved in a meal. We declare the method as final so that it can't be overridden. The algorithm defined by doMeal() consists of four steps: prepareIngredients(), cook(), eat(), and cleanUp(). The eat() method is implemented although subclasses can override the implementation. The prepareIngredients(), cook(), and cleanUp() methods are are declared abstract so that subclasses need to implement them.
Meal.java
package com.cakes; public abstract class Meal { // template method public final void doMeal() { prepareIngredients(); cook(); eat(); cleanUp(); } public abstract void prepareIngredients(); public abstract void cook(); public void eat() { System.out.println("Mmm, that's good"); } public abstract void cleanUp(); }The HamburgerMeal class extends Meal and implements Meal's three abstract methods.
HamburgerMeal.java
package com.cakes; public class HamburgerMeal extends Meal { @Override public void prepareIngredients() { System.out.println("Getting burgers, buns, and french fries"); } @Override public void cook() { System.out.println("Cooking burgers on grill and fries in oven"); } @Override public void cleanUp() { System.out.println("Throwing away paper plates"); } }The TacoMeal class implements Meal's three abstract methods and also overrides the eat() method.
TacoMeal.java
package com.cakes; public class TacoMeal extends Meal { @Override public void prepareIngredients() { System.out.println("Getting ground beef and shells"); } @Override public void cook() { System.out.println("Cooking ground beef in pan"); } @Override public void eat() { System.out.println("The tacos are tasty"); } @Override public void cleanUp() { System.out.println("Doing the dishes"); } }The Demo class creates a HamburgerMeal object and calls its doMeal() method. It creates a TacoMeal object and calls doMeal() on the TacoMeal object.
Demo.java
package com.cakes; public class Demo { public static void main(String[] args) { Meal meal1 = new HamburgerMeal(); meal1.doMeal(); System.out.println(); Meal meal2 = new TacoMeal(); meal2.doMeal(); } }The console output of the execution of Demo is shown here.
Console Output
Getting burgers, buns, and french fries Cooking burgers on grill and fries in oven Mmm, that's good Throwing away paper plates Getting ground beef and shells Cooking ground beef in pan The tacos are tasty Doing the dishesAs you can see, the template method design pattern allows us to define the steps in an algorithm and pass the implementation of these steps to subclasses.
No comments:
Post a Comment