Template Method Design Pattern in Scala

1. Definition

The Template Method Design 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.

2. Problem Statement

Imagine you have a sequence of operations that needs to be executed in a particular order, but some steps in the middle might vary depending on the context. Manually handling this using conditional statements can become complicated and error-prone.

3. Solution

The Template Method Pattern suggests moving the invariant parts of the algorithm to a separate abstract class, while specific steps are implemented in its subclasses. This ensures the overall structure remains consistent while allowing flexibility in the steps' implementation.

4. Real-World Use Cases

1. Implementing benchmark tests where setup and teardown methods are common but the actual test varies.

2. Different recipes for baking, where the baking process remains the same, but ingredients or preparation steps may differ.

5. Implementation Steps

1. Create an abstract class with a template method being final.

2. The template method defines the order of execution of operations.

3. Some steps are implemented directly in this class (common steps) and some are abstract, requiring derived classes for implementation.

6. Implementation in Scala Programming

// Step 1: Define the Abstract Class
abstract class BreadRecipe {
  // This is the template method
  final def prepareBread(): Unit = {
  // These are default steps
  def mixIngredients(): Unit = {
    println("Mixing ingredients for bread.")
  def bake(): Unit = {
    println("Baking the bread.")
  // This step will vary, so we keep it abstract
  def addToppings(): Unit
// Step 2: Implement concrete subclasses
class CheeseBread extends BreadRecipe {
  def addToppings(): Unit = {
    println("Adding cheese topping.")
class GarlicBread extends BreadRecipe {
  def addToppings(): Unit = {
    println("Adding garlic butter.")
// Client and Tester
object TemplateMethodClient extends App {
  val cheeseBread = new CheeseBread
  val garlicBread = new GarlicBread


Mixing ingredients for bread.
Baking the bread.
Adding cheese topping.
Mixing ingredients for bread.
Baking the bread.
Adding garlic butter.


1. The BreadRecipe abstract class defines the structure of the algorithm in the prepareBread method. This is the template method.

2. Certain methods like mixIngredients and bake have default implementations. These are the invariant parts of the algorithm.

3. The addToppings method is abstract, indicating that this part of the algorithm can vary and should be provided by subclasses.

4. CheeseBread and GarlicBread are concrete implementations of the BreadRecipe. They provide their own implementation for the addToppings method.

5. When the client code calls the prepareBread method, the sequence of steps is executed in order, but the toppings step varies based on the object type.

7. When to use?

Use the Template Method pattern when:

1. You want to let clients extend only particular steps of a large algorithm, making them optional or mandatory as required.

2. You have several classes that contain almost identical algorithms with some minor differences. As a result, you might need to modify all classes when the algorithm changes.