Learn the Singleton Design Pattern

Singleton is a creational design pattern that lets you ensure that a class has only one instance while providing a global access point to this instance.

  1. Ensure that a class has just a single instance — The most common reason for this is to control access to some shared resource like database access.
  2. Provide a global access point to that instance — The singleton pattern lets you access the same object from anywhere. It also protects that instance from being overwritten by other codes.
  • Make the default constructor private, to prevent other objects from using the new operator with the Singleton class.
  • Create a static creation method that acts as a constructor. Under the stood, this method calls the private constructor to create an object and saves it in a static field. All following calls to this method return the cached object.

UML Class Diagram

Not familiar with UML Class Diagram? I have written a detailed post on the UML Class diagram.

Implementation steps:

  1. Add a private static (volatile for multi-threading) field to the class for storing the singleton instance.
  2. Declare a public static creation method for getting the singleton instance.
  3. Implement a lazy initialization inside the static method. It should create a new object on its first call and put it into the static field. The method should always return that instance on all subsequent calls. You need to take care of thread lock if your application is multithreaded.
  4. Make the constructor of the class private. The static method of the class will still be able to call the constructor, but not the other objects.

Using Binary Semaphore

// Output on my system
pool-1-thread-2: Instance is null before acquiring semaphore
pool-1-thread-2: Semaphore acquired
pool-1-thread-1: Instance is null before acquiring semaphore
pool-1-thread-2: Instance is null after acquiring semaphore
pool-1-thread-2: Semaphore released
pool-1-thread-2: You are testing Singleton Class: 1258930662
pool-1-thread-1: Semaphore acquired
pool-1-thread-1: Semaphore released
pool-1-thread-1: You are testing Singleton Class: 1258930662

Using Synchronized

// Output on my system
pool-1-thread-2: Instance is null before synchronized
pool-1-thread-1: Instance is null before synchronized
pool-1-thread-2: Instance is null after synchronized
pool-1-thread-2: You are testing Singleton Class: 1962484795
pool-1-thread-1: You are testing Singleton Class: 1962484795

Using enum to create Singleton

// Output on my system
pool-1-thread-2: You are testing Singleton Class: 1962484795
pool-1-thread-1: You are testing Singleton Class: 1962484795

Pros of Singleton design pattern

  • You can be sure that a class has only a single instance.
  • You gain a global access point to that instance.
  • The singleton object is initialized only when it’s requested for the first time.
  • Saves memory for the creation of multiple objects.
  • Handles the concurrency issues in a multi-threaded environment.

--

--

--

“Walking on water and developing software from a specification are easy if both are frozen”

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Effective Tool Integration in ITSM can Boost Productivity — Part 2

How to read multiple CSV file from S3 location using Lambda function

What’s inside the Barcode?

Setting up a Mongo instance in AWS EC2

Designing A Simple Event Source

☁️ Using external libraries in your Python AWS Lambda in AWS CDK

Top 3 Programming Languages to Learn in 2022:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Neeraj Kushwaha

Neeraj Kushwaha

“Walking on water and developing software from a specification are easy if both are frozen”

More from Medium

Learn GoF Design patterns to solve problems in software design

A brief introduction to Clean Architecture

What are REST API’s constraints?

Singleton Design Pattern