Template Method Pattern using C++ (Pizza Example)

What is the Template Method Pattern?

The definition of the Template Method Pattern from both Design Patterns: Elements of Reusable Object-Oriented Software and Head First Design Patterns: A Brain-Friendly Guide is

The Template Method Pattern defines a 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

Template Method Pattern Class Diagram Explained

Template Method Pattern

Let’s have a look at the generic class diagram of the Template Method pattern. It consists of two classes, AbstractClass and ConcreteClass. ConcreteClass inherits from AbstractClass. The AbstractClass implements the TemplateMethod() as seen in the yellow box and should be considered the final implementation, meaning subclasses can not override it. The method implements the skeleton of an algorithm that can include calls to one or more abstract methods. In this diagram there are two, PrimitiveOperation1() and PrimitiveOperation2(). These methods must be implemented by the ConcreteClass. Additionally, there is a virtual method call Hook(). A hook method has a default implementation in the parent class but allows subclasses to override it.

Template Method Pattern Example

Let’s consider applying the Template Method pattern to the steps needed to create a pizza. First thing you need to do is prepare the dough. Once that is done you would then add the sauce. Step three would be to add any additional toppings. Lastly, you would bake a pizza. So we have our algorithm for template method. Remember our template method would be defined in our AbstractClass. Let’s name that Pizza and call our template method PreparePizza(). This method would call our step methods, PrepareDough(), AddSauce(), AddToppings() and Bake(), forming our algorithm. For simplicity, let’s say there are only two types of Pizza subclasses (ConcreteClasses), CheesePizza and MeatLoversPizza and that they both have the same dough, sauce and instructions for baking. This means we can implement PrepareDough(), AddSauce() and Bake() as part of our Pizza object. These could be considered Hook() methods. AddToppings() will need to be implemented by our subclass objects since the toppings are different for each subclass. Below is a class diagram depicting our example.

Template Method Class Diagram

Example written in C++

So we have our design finished. Let’s take a look and see what our implementation will look like. We’ll start with Pizza. Notice that the Prepare() method is not virtual. We purposely don’t want our subclasses to override this behavior. The step methods are virtual and protected where AddToppings() is pure virtual as we want our subclasses to define this implementation.

Pizza.h

Pizza.cpp

CheesePizza.h

CheesePizza.cpp

MeatLoversPizza.h

MeatLoversPizza.cpp

PizzaTest.cpp

When we run this code we see this output:

Preparing a Cheese Pizza...
preparing dough
adding sauce
adding cheese topping
bake pizza

Preparing a Meat Lover’s Pizza…
preparing dough
adding sauce
adding cheese, pepperoni, sausage and bacon toppings
bake pizza

Note that the only variant, as expected, is the addition of toppings.

You can find all of the code here

Benefits of the Template Method Pattern

The main benefit the Template Method Pattern provides is code reuse. The parent class keeps the structure of the algorithm the same within the template method and any variations are implemented by a subclass.

Recommended Resources

Here are some resources I recommend for more information on the Template Method Pattern

Head First Design Patterns: A Brain-Friendly Guide

Design Patterns: Elements of Reusable Object-Oriented Software

In Closing

There you have it. If you followed along, I hope you now have a better understanding of the Template Method Pattern. Please feel free to leave comments and feedback. Much appreciated!

One Comment

  1. Shourya

    Actually was looking for IOC and template pattern relationship .Got you article
    Liked your explanation here ..Can you please add/tell about IOC with template pattern ?

Comments are closed.