Template Method pattern — get the gist in 2 min

aditya chaudhari
JavaDeveloperDiary — JDD
3 min readAug 23, 2022
Template method pattern — java

The main intent behind the template method pattern is to provide the skeleton of an algorithm in the parent class and let subclasses extend the parent and define fill-ins by overriding methods.
Well, the interesting part is that though algorithm fill-ins are implemented in the subclasses they are called by the parent class. Only parent calls calls those methods from subclass and this is called as famous “ Don’t call us, we will call you” Principle.

Template Method is the type of behavioral design pattern where inheritance is used to describe the algorithm and flow of control.
Superclass defines the default and abstract methods and lets subclass provides an implementation of abstract methods.

An inheritance is at the heart of template method pattern and template methods are fundamental techniques for code reuse.

Simple Example :
Consider you are implementing a place order feature in an eCommerce site, where place order can be of three types web-order, web-pickup-instore-order & In-store order.
All these three share a common method which are doBasicValidations() and createOrderInERP() but all of them have different behaviors for delivery address and payment.
The goal is to provide solutions that focus on reusability and extensibility.

Refer solution design below :

Template method pattern simple code example design structure

click here for complete github code.

Structure and different components in decorator pattern :
The template method is defined in the abstract class which acts like a gateway to the algorithm and lets the subclass defines some parts of the algorithm by providing implementation to abstract methods.

template method pattern structure

A concept of hook in the template method pattern :
The hook is the method that is declared in the abstract class but only given an empty or default implementation.

As a rule, when subclass must provide an implementation of the method or step in the algorithm then use abstract subclasses and use hooks when part of the algorithm is optional. With hooks, a subclass may choose to implement that hook, but it doesn’t have to

Usage of template method pattern in java libraries/framework :
This pattern is a favorite for framework designers, because of the superpower it provides to the implementer of the framework. The ability to provide extensibility as well as code reusability at the same time is the superpower.

In the Spring framework, JdbcTemplate, RestTemplate are examples of template patterns they provide code reusability by abstracting the low-level boilerplate code as well as providing the callbacks which help alter the algorithm logic defined in the parent class.

The real fun is in the details, if you wish to know and understand the pros-cons, java-code example for the decorator pattern then please consider checking the detailed article at [template-method-pattern-explained-5-min-read]

Thanks for reading. Love IT, Live IT, Enjoy IT.

On the journey to understand the core essence of design patterns.
Every Tuesday I publish a small 2 min post and a long 5–10 min post along with a code example, please consider follow & subscribe if you wish to get notified!

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT