Turkish Green Beans Recipe

--------------------------

1 - Boil some water

2 - Add salt

3 - Add green beans

4 - Add some onions

Notice that the instructions of both recipes look very similar. As a matter of fact, these instructions are algorithms.

Let’s translate them into code:

Okra and Green Beans classes for dinner preparation

Pay attention that if we copy the boilWater() and addSalt() instructions to every new recipe over and over again we’ll end up with duplicated code.

So, how can we make it better?

Remember the words of the ancient god of design principles:

Encapsulate what varies

So what is varying here? The non-abstract states of the algorithm!

Template Method pattern encapsulates algorithms

prepareRecipe() methods are different only because of their 3rd and 4th method calls.

Maybe we can merge them in a different method.

Add okras and green beans can be abstracted into addVeggie(). Same can be done with onions and tomato sauce as addFlavor().

Now our recipe looks like this:

Veggie Recipe

--------------------------

1 - Boil some water

2 - Add salt

3 - Add veggie

4 - Add flavor

But how can we reflect that in our code?

The prepareRecipe() is our template method

Note:

Now prepareRecipe() can be used to make both Green Beans and Okras.

prepareRecipe() is final therefore our subclasses cannot override this method. This ensures the algorithm cannot be changed.

Veggie class performs the first two steps but relies on Green Beans and Okras to handle the remaining ones. For this reason we left addVeggie() and addFlavor() as abstract methods.

We have successfully implemented the pattern. Let’s see how it works:

It works! Now we can officially give a definition for the pattern. Directly copying from Head First Design Patterns[1]:

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.

Here is the class diagram. (Don’t care about the hook method now, I’ll talk about it soon.)

The Template Method Pattern Class Diagram

Template method defines the algorithm skeleton and calls concrete operations, primitive operations and hooks.

Primitive operations must be overridden by subclasses.

Superclass defines the default behaviour of Hooks (usually skips). Then subclasses may override them.

Hook Method

Let’s say adding tomato sauce to the okras is optional. We can achieve it by using hooks. The parent class will provide the default implementation as empty and if the subclass wants to use it, it can override it.

Notice that although concrete classes are final, hook class isn’t final so that subclasses can override it.

Now You’re Free

Now, you are graduating from Template Method Design Pattern Program. You can go and show off your knowledge to your developer friends.

Don’t stop learning design patterns. It’ll make you a better developer than your peers.

References

[1] FREEMAN, E., ROBSON, E., SIERRA, K., & BATES, B. (2004). Head First design patterns. Sebastopol, CA, O’Reilly.

Interested in more posts?