This is a space that holds a collection of my personal work and ideas

Behavioral Patterns - Template Method Pattern Exercise

Posted on 07/11/2018

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.

Reading Materials

  1. https://en.wikipedia.org/wiki/Template_method_pattern (10 minutes reading)

Practice Materials

  1. 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:

  1. Abstract Class (The abstract class that defines the steps of algorithms and abstract method and hooks for subclasses to implement) - ThreeCourseDinnerTemplate
  2. 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.

Solution

Three-Course Dinner Example

Questions to discuss

  1. What are the common use cases to apply the Template Method Pattern?
  2. What are the differences between the Template Method Pattern and the Strategy Pattern?

Read on GitHub