Mahipal Nehra

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

Contact the author

Like Share Comment
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
Like Share Comment
Comments

Articles from Mahipal Nehra

View blog
1 year ago · 1 min. reading time

We are living in a world that is on the verge of Industry 4.0 or the fourth industrial revolution. A ...

2 years ago · 1 min. reading time

5G is the most recent upcoming remote system innovation that is being executed in certain urban netw ...

1 year ago · 1 min. reading time

Developers have been using three major building blocks for the development of web apps, namely, APIs ...

You may be interested in these jobs

  • Tata Group

    Historian

    Found in: Talent IN - 3 days ago


    Tata Group Bengaluru

    Must-Have** · (Ideally should not be more than 3-5) · Experience working on Inmation (Data Historian) application software · Experience in managing a MongoDB (or comparable database) infrastructure, preferably in a complex, technical environment. · Strong understanding of indus ...

  • Oracle

    Technical Analyst

    Found in: Talent IN - 2 days ago


    Oracle Noida Regular Employee

    As a member of the Support organization, your focus is to deliver post-sales support and solutions to the Oracle customer base while serving as an advocate for customer needs. · This involves resolving post-sales non-technical customer inquiries via phone and electronic means, as ...

  • Twelve Data

    Senior Software Engineer, Frontend

    Found in: Talent IN - 4 days ago


    Twelve Data Bengaluru

    What we need · An experienced front-end developer is required in the core development team. The team develops products that automate processes within the company, develops pages and components of the site, improves site performance and customer experience. We make a high-quality ...