Implementation of the Design Patterns in Java and Spring Framework

Ali Zeynalli
2 min readJul 19, 2021

--

Photo by Oksana Berko on Unsplash

Template Design Pattern is one of the most used behavioral design patterns. The main idea of this pattern is having an abstract class with final method which is template method and bunch of different sub-steps that are abstract and so free to be implemented. Basically, in this pattern the functionality of a template method can be modified by differently implemented abstract methods. For a better understanding let us take a glance at following cashflow uml diagram.

UML Diagram for a Template Method Example

In above given example, we have got an abstract CashFlowTemplate class that has a final calculateCashFlow template method which calls two further abstract methods. Those methods are implemented by children classes of CashFlowTemplate class (IncomeService, ExpenseService). In Demo class we can choose which implementation we should take for template method, but instantiating either Income- or ExpenseService. Here you can take a look at a similar example in my git account.

Template Design Pattern is a beloved pattern for framework developers since this pattern reduces boilerplate code. Spring also is here no exception. So for cases, where there are repeated actions or situations where different sub-functionalities should be implemented by user, this design pattern is perfect match. Have a look at following template implementations in Spring:

P.S. You can connect with me on twitter or linkedin.

--

--

Ali Zeynalli

IT-Architect | BMW Group | M.Sc. Technical University of Berlin