Design patterns: Template Method

Hello everyone!! After few months, I’m again! 😃😎🙂 I had a lot of others activities. Today is about the template method design pattern. Is a lot of to describe but about this is further in the post. 🙂

 

Admission

I was gone for a long time, but I probably needed some rest. Is a lot of to talking, example I will be starting my own company in IT branch in not so far future, I was talking with some people about few ideas mobile applications, and we have a few expectant investors, so I hope that will be successful 🙂 but we’ll see, still is a lot of time on that 🙂 But enough about me as you see I had a lot of work. Let’s back to the topic, today is about the template method design pattern, which in brief is used as a program skeleton. I feel like Arnold 😂😂

 

Discussion

The design pattern consists of at least two classes. In the AbstractClass class an program skeleton is defined and it is used by the client, there are methods that are abstract, so there are variables and we can change their behavior in each class that inherits from the AbstractClass class, there are also methods that have already defined logic, that is, they are immutable, their behavior can not be changed. And all these methods are called in the templateMethod() method, which is also defined in the AbstractClass class.

The template method is used very often in various frameworks. If someone noticed the connection template method with the Inversion of Control principle, it congratulates the perceptiveness 🙂. Note that the AbstractClass class is, you can say the class framework is defined in it, the logic is used by the client and the code in this class calls the client code, i.e. complies with the Hollywood principle.

 

Intent

  • Defining the skeleton of the algorithm.
  • Separating part of the logic to classes that inherit from the AbstractClass class (base class) without changing the basic structure of the algorithm.
  • Implementing invariant part of the algorithm.

 

Problem

You have created some part of the code that is hard to expand and you want this code to be easy to modify, or you have a code that has many similarities, but also defines common implementations or interfaces that can not be used. In all these cases, it is worth to use the template method 🙂 design pattern.

 

Use when:

  • You want, for example, to have a program that will be open to extensions, but closed to modifications, i.e. you want to have a piece of the program that will be expandable without changing the existing logic, i.e. the template method pattern also applies the second solid principle. 🙂
  • You create your own framework that other people will use, passing some arguments to it and that will return a specific answer.

 

Structure

In this section, I will show the UML diagram of the template method:

template method scheme

The whole code that you can use again is in the FrameworkClass class and the classes that extend the framework logic are ApplicationClassOne and ApplicationClassTwo.

The template method is also used in sorting algorithms, the implementation principle of which is described in the diagram below:

template method scheme

The logic part of the sorting algorithm is invariant, such as the sort() or returnArray() methods, because they always work the same way, there is no need to expand anything, only the compare() method is extended to add sorting descending and sorting ascending.

Let’s see now how it looks like in the code, first we will see what the AbstractClass class looks like, ie the most important class in the pattern:

And let’s see the classes that inherit from the AbstractClass class.

We see that the abstract methods that are defined in the AbstractClass class are adapted to the classes that inherit from the AbstractClass class.

And finally, the customer.

In the client, we call the TemplateMethod() method, which calls all the other methods in the AbstractClass class, we see the using of the IoC principle, framework invokes the client’s methods.

Result

template method scheme

 

Example

An example of a template method is the employee’s daily routine.

template method scheme

We can see here that some of the activities of some workers are the same, others are not. In this example, it’s a good idea to use the template method. Let’s see how it is looks like in the code. Let’s start from the main class AbstractClass, in this example it is the Worker class.

This class is similar to the AbstractClass from the previous example, but that class is extended by invariant methods goToWork(), work(), returnToHome() the rest of the code is similar to the previous example. We assume that all workers go to work, work and return to home at the same hours, so there is no need to make this methods as a changeable.

Let’s see now the FireFighter, Lumberjack, Postman and the Manager classes.

FireFighter class

Lumberjack class

Postman class

Manager class

As you can see, these classes only extend the methods getUp(), eatBreakfast(), relax(), sleep(), each employee adjusts the remaining hours of activity to himself.

At the end the client.

And we call the DailyRoutine() method in each class.

Result

template method scheme

 

Real-life example

Real-life example can be the document generator. In the console we will make generators of HTML and XML documents. In order as in the previous example, let’s start from the main class, in this example main class is the DocumentGenerator class.

Nothing new 🙂

Let’s see the classes that extend a generateHeader(), generateBody() and generateDetails() methods.

First HTMLDocGenerator

And XMLDocGenerator class

And the client

This is a simple example, I think there is nothing to explain 🙂 If I’m wrong give to know in the comments.

Result

template method scheme

 

Relations with other design patterns

  • Template Method uses inheritance to vary part of an algorithm. Strategy uses delegation to vary the entire algorithm.
  • Strategy modifies the logic of individual objects. Template Method modifies the logic of an entire class.

 

Summary

That’s all about Template method🙂.

Link to github with the whole code from this article:  https://github.com/Slaw145/TemplateMethodTutorial

This content also you can find on my steemit blog  https://steemit.com/design-patterns/@slawas/design-patterns-template-method

And on medium: https://medium.com/@sawomirkowalski/design-patterns-template-method-45888a2b84bc

In the next article, we will talk about the Observer pattern.

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb: Devman.pl-Slawomir Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

Now a lot is happening with me, so entries will be more or less every two to three weeks, on the fb page and on the group about every two. I will send newsletters more or less every two weeks, so many things are happening that it’s hard to predict when exactly I will do it. 🙂

Illustrations, pictures and diagrams are from: https://sourcemaking.com/design_patterns/template_method

 
If that post was useful for you share it with your friends :)

Post a comment

Be first!

avatar
  Subscribe  
Notify about