Behavioral Patterns - Template Method Pattern Exercise


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. (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.


  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


  • 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.


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?


Popular posts from this blog

Creating a Library Project for Spring Boot

Spring Data Repository Query Precedence Tricks

Overwriting Spring Security Context through Filter