Design Patterns: Template Method

Hi guys, I am going to continue with behavioral design patterns and today I will introduce you the Template Method pattern.

Template Method pattern provides a template method that defines a set of steps (it calls other methods) and the implementation of steps (methods) can be formed in sub-classes if a step has varied implementations. If not, then it’s a good practice to implement this step in super class.

According to Gang Of Four Book :

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 algorithms structure.Gang of Four

As you can imagine, in order to create a template method pattern, an abstract class is mandatory. We can use an abstract class in order to create the template method with the steps, we can create steps if they are stable and also create abstract methods for steps that are changing depending on circumstances.

Why Template Method Pattern?

  • When there is an algorithm with steps that may vary.
  •  Because we can avoid duplicate code and we can use inheritance through a subclass in order to use the same code.
  •  We can create a skeleton of an algorithm that helps us to simplify the complexity.

It is very easy to create a template design pattern, so, it would be better to continue with an example.

EXAMPLE

Let’s start with a simple example. We have some users who have access to a platform to fill out a test. After test completion, we extract some scores. So, we have to create a system that computes the score in two different scenarios.

At first, we will create an abstract class named Scoring with 3 methods. The first one is always a standard method “computeScore()” that returns the sum of the 2 others methods. The computation1() and computation2() methods are abstract and we are going to implement two different scenarios.

Let’s continue with the first scenario. We create a class Scenario1 which is a subclass of Scoring abstract class.

Continuing with the second scenario and Scenario2 class.

As you can see, we have two totally different implementations of computation1() & computation2() methods in our classes. These computation approaches do not have an impact in real life, I just wrote the first thing that came to my mind.

Now we are ready to call these 2 classes.

Output:
The score from scenario 1 is: 70.0%
The score from scenario 2 is: 38.0%

Caveat: As you can see, we have an abstract class, and we extend this class in order to implement some methods. We are able to use another abstract class which is going to extend the first abstract class and so on.

When you catch yourself to act like that then STOP immediately or be careful. Do not use more than two abstract classes. In my opinion do not use more than one abstract class because you are going to lose instead of gaining something from inheritance.

The reason is simple. The code becomes hard for debugging, you add more complexity and complexity is never good, difficult to maintain the code and even more difficult to add new functionalities. Also, it’s a good practice to restrict the usage of this pattern because it doesn’t fit very well to the open/closed principle.

Also, you can read this article so as to get a better insight on why Template Method pattern is a bad practice.

You can download the source code from GitHub.