Template Method Pattern

It’s all about algorithms, right? Ever since the word computer came to exist, word algorithm was behind it. Indeed, whenever you develop something, you define steps how to accomplish any given task. There are many ways to complete any task: a lot of poor ones, a few good, and one or two great ones. We are here on the quest to find out the great ways to do that, when we develop an application. On top of defining a good algorithm, we also want to make sure that we can potentially reuse it, in case we would need to extend our current system Let’s examine an example.

Say we want to start creating paintings. We need to to define an algorithm of how we would go about it:

  1. At first we would need to prepare the canvas.
  2. Prepare the paints and brushes, makes sure you’ve got them.
  3. After that we are ready to paint a picture.
  4. When we are done painting, we should frame the picture.
  5. Our painting is ready!

There we have it – a four step process. You might have thought already that painting step is not really defined: what to paint? That is something that can be different from case to case. We don’t want to end up painting the same picture over and over again. This part of algorithm is something that will be different every time. We will need to make sure that whoever uses this algorithm will define what they are going to draw. To do that we can put our algorithm into an abstract class, and then leave paint() method abstract, while others stay defined. This would force classes that inherit from this class to implement painting step while not needing to worry about others, because they stay the same.


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.


Template Method is that easy and simple: you define the structure of an algorithm, and make children classes implement certain steps. In our case, we make children classes implement method paint(). In the end we can have some MonaLisaPainting class that subclasses Painting class with its defined paint() method. Hopefully Leo knows how to implement his paint() method 🙂

There can always be several methods that a subclass might be forced to implement, for example we can also make frame() method abstract and make subclass define what kind of frame to apply to the painting. Additionally we can define something called “hook” in our abstract class. A hook is a method that is declared in the abstract class, but given empty or default implementation. For example, we can have an extra method pack() in our algorithm that would be responsible for packing up the painting for transporting or sale. We do not always need that to be the case: sometimes we just want to make a painting for ourselves and hang it on a wall. What we can do is to introduce a hook named isPacked(), and give it default implementation that would return true. If a subclass doesn’t want to get to packing stage, they simply override isPacked() method returning false and that’s it – no packing for this one! Pretty handy way to deal with algorithm conditionals.

Template Method allows us to keep general structure of the algorithm to be the same for all classes that extend this one, while allowing for implementation of a particular step in the algorithm. It can be applied in a lot of step by step production algorithms where one step is different from case to case, or can require creative approach.

One thought on “Template Method Pattern

Leave a comment