The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. In this exercise, we will use the Template Method Pattern to create a Three Course Dinner Template for training restaurant staff for preparing the meal.
Use the Template Method Pattern to create a Three-Course Dinner Template that meets the following requirements:
It defines a set of steps in the following order:
beforeStart (hook)
serveBeverage (optional)
makeBeverage (only if serveBeverage is true)
makeStarter
makeEntry
makeDessert;
afterFinish (hook)
Make two concrete three-course dinners by using the Three-Course Dinner Template.
Components:
Abstract Class (The abstract class that defines the steps of algorithms and abstract method and hooks for subclasses to implement) - ThreeCourseDinnerTemplate
SubClass (The implementations of the ThreeCourseDinnerTemplate) - SurfNTurfDinner, VegetarianDinner
Tips
A hook may provide a way for a subclass to implement an optional part of an algorithm, or if it isn’t important to the subclass’s implementation, it can skip it. Another use is to give the subclass a chance to react to some step in the template method that is about to happen or just happened.