5 Software Design Patterns implemented in Spring

Singleton, Prototype, Factory, Proxy, Template Design Patterns in Spring

Ali Zeynalli
6 min readSep 25, 2021
Photo by Bogdan Karlenko on Unsplash

21 Design Patterns that, are gathered and introduced to bigger computer science audience by GoF, are handy patterns for different use cases. Spring itself makes use of some of them in its core implementation. These are: Singleton, Prototype, Factory, Proxy, Template Design Patterns. Let us have a look at each of them in java and than spring context:
Singleton…

This pattern is part of creational design patterns and deals with special form of object creation. It basically restricts the instantiation of a specific object to more than one. It is especially useful in cases, where there should be single shared instance of an object such as DB connections, logging system or configuration manager, caching manager.

The basic idea is having private constructor and a getter that delivers same instance of an object. In following example we have three kind of Singleton method implementations:

  1. in classical implementation we have got a private constructor and a getInstance() method that delivers the same object every time. Problem here is: if we have multiple threads instantiating the class, every thread will create isolated singleton object.

--

--

Ali Zeynalli

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