Template Method Pattern.

Farida Aliyeva
4 min readDec 17, 2020

--

An architectural plan is prepared so that it can be altered to fit the client’s needs.

Servus, my dear loyal fans, and here we are again discussing some fancy patterns and their use. And for today we have a Template Method Pattern. Let us get it started…

Template Method Pattern

The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. Template means Preset format like HTML templates which has a fixed preset format. Similarly in the template method pattern, we have a preset structure method called template method which consists of steps. These steps can be an abstract method that will be implemented by its subclasses.

This behavioral design pattern is one of the easiest to understand and implement. This design pattern is used popularly in framework development. The reason is that it helps to avoid code duplication also.

Let us look at an example of Template method pattern implementation in Java. So the task is to create software to build the houses. Sounds weird but as we have already noticed Template Method is ideal for contribution plans thus it is quite logical mhm…

Well, first of all, let us have a look at the UML Diagram for pseudo-software.

UML Diagram.

Here we have an Abstract class called SummerHouse which indicates that we are going to construct a 1–2 storey house. And then we have basic Java classes that represent various House styles namely: High-tech and Classic. Both classes extend the Parent Abstract class which means they are indeed inheriting its properties and functionalities. But despite that, the implementation of the methods is kept up to Child classes. Well, it is hard to get that without having a look at a code.

Parent class; Abstract Class; SummerHouse

From the picture inserted above, we can see several unimplemented abstract methods and some public methods of our parent class. The crucial one is the constructHouse() which will run all the methods required for house construction. Additionally, it checks for the customer specifications in case there is one.

High-tech Class.

Here is the essential chunk of the High-tech class where we can see all the methods from SummerHouse (Parent class) to be Overriden and adds the implementations to abstract methods. Hook if a user wants a stone modern garage to be built next to the house.

A hook is a functionality provided by software for users of that software to have their own code called under certain circumstances. That code can augment or replace the current code.

Classic Class.

Same as in High-tech but some new implementations…Child class that extends the functionality of a SummerHouse and adds the implementations to abstract methods. Hook: if a user wants a nice garden with decorations to be built next to the house.

Before we move to the output of the code let us have a look at the Hooks in both classes.

Classic Class Hook Implementation.
High-tech Class Hook Implementation.

Now, it is time to check the results of the Template method pattern implementation.

Testing.

Results:

Output.

And here we go…it works perfectly…my sincere congratulations. Now that you have learned what the Template Method Pattern is and how to implement it, use your imagination and think of a situation where you might have to need it and try it out. Let me give you a hint imagine you would like to develop an editor that enables you to extract the text in various formats? Did you get the idea, huh ? Well, then hurry up to code it :)

References:

Book- Eric Freeman, Elisabeth Freeman, Kathy Sierra, Bert Bates-Head First Design Patterns -OReilly (2008)
Feel free to check my github repository where I posted several design patterns implemented in Java — https://github.com/Farida004/Software-Design-and-Patterns

Thank you for your precious time ;) Good luck!

--

--

Farida Aliyeva

Data Scientist at SDH | MS Graduate in Computer Science and Data Analytics