Mahipal Nehra

3 years ago · 2 min. reading time · ~10 ·

Blogging
>
Mahipal blog
>
Template Method Design Pattern Java Example

Template Method Design Pattern Java Example

Decipher Zone Softwares

+ Template Method()
+ Stepl()
Step2()

Concrete ClassA Concrete ClassB

+ Stepl() * Stepl()
Step2() Step2()
Step3() Step3()

 

What is Template Design Pattern?

https://decipherzone.com

Template method design pattern is a behavioural design pattern that is concerned with algorithms and the assignment of responsibilities between objects, as do all patterns that fall in this category. The speciality of behavioural design patterns is that they not only describe patterns of objects and classes but also the patterns of communication between them. You might have heard of encapsulating object creation, method invocation and other complex interfaces. What you might not have heard of is encapsulating algorithms. Template method design pattern is all about that. In this article, we will discuss what is Template method design pattern, why it is used, and a simple Template method design pattern java example to demonstrate its use case.

What is Template method design pattern?

Template method design pattern is a design pattern that provides an abstract definition of an algorithm with the help of an abstract method called Template Method. Like most of the behavioural design patterns, Template method design pattern uses inheritance to distribute behaviour between classes. In this pattern, each step invokes either an abstract or a primitive operation. A subclass provides additional details to the algorithm by defining the abstract operations. So, what is Template method design pattern’s intent? The intent is to define the skeleton of an algorithm by postponing some steps to subclasses i.e. it lets subclasses define certain steps of an algorithm in their own way keeping the structure of the algorithm intact.

Read also: "Singleton Design Pattern – A thoughtful dive into Object Structures and Creation"

Why do we need Template method design pattern?

Using design patterns such as the Template method design pattern, we can reduce redundancy in code. We will see an example of it soon. Template methods are the basic techniques of code reusability. If we are to design a framework or a library, there obviously will be common behaviour between classes that have similar structure and purpose and to deal with that we need a design pattern such as this one. In our case template methods are the means of factoring out common behaviour in library classes and frameworks. If you have ever used frameworks like angular, you have certainly come across injecting functions to controllers i.e. use them only when needed or “hook” them in and that will be called by angular itself. Such patterns lead to a principle that says, “Don’t call us we will call you”. Systems designed with such patterns allow low-level components to hook themselves into the system and the high-level components determine when and how they are needed. This refers to how a parent class calls the operations of a subclass and not the other way around.

Read also: "All you need to know about Template Method Design Pattern"

Let us see an example of the Template method design pattern in java.

Template Method Design Pattern Java Example

We will make use of a very simple example of making mango and orange juices. The algorithms are straightforward and have been written as method calls in respective classes. First, we will do it the amateur way and then see how the template method can abstract out some of the algorithmic features from the intended classes.

Read the full article:  https://www.decipherzone.com/blog-detail/template-method-design-pattern
Comments

Articles from Mahipal Nehra

View blog
3 years ago · 1 min. reading time

Top 10 IT skills that will be in demand in 2021. Every passing year the tech world grows and transfo ...

3 years ago · 1 min. reading time

The software development industry progresses through continuous evolution where people try to solve ...

3 years ago · 1 min. reading time

Looking back to the era where there was no internet is quite difficult. With more and more devices c ...

You may be interested in these jobs

  • BRFM India

    Sales Manager

    Found in: Appcast Linkedin IN C2 - 6 days ago


    BRFM India Dodda Ballapur, India

    Position: Sales and Marketing Manager - Flour Mill / Food Industry · Location: Bangalore, India · About Us: · BRFM India has put up a new flour mill in Bangalore. It is known for its high-quality products and commitment to sustainable practices. We serve both the commercial bakin ...

  • Maxeon Solar Technologies

    Senior Helpdesk cum NOC Engineer

    Found in: Appcast Linkedin IN C2 - 6 days ago


    Maxeon Solar Technologies Hyderabad, India

    Powering Positive ChangeTM · In a world of constant change, the only way to make a meaningful impact is to stay ahead of the curve. That's why at Maxeon Solar Technologies we've been pushing the boundaries of solar innovation every day for 35 years – from the very edge of outer ...

  • A2MAC1 - Decode the future

    Technical Account Manager

    Found in: Appcast Linkedin IN C2 - 6 days ago


    A2MAC1 - Decode the future Chennai, India

    Our continued growth, success, and investment have led to the rapid expansion of our global team, especially with the great expansion plans in Chennai, India, a benchmarking site, empowering with wider production functions, globalized technological centre and the costing centre, ...