Introduction to Design Pattern-Singleton

Issath Sesni
3 min readMay 23, 2021
Image by Refactoring.Guru

What is Design Pattern and Why

When a solution gets repeated over and over again in several projects, then someone puts a name to it and describes the solution in detail. That’s how basically a pattern gets discovered. So, Design pattern is a general repeatable solution. And It is used for commonly occurring problem in software design.

It was initiated by Gang of Four in 1994 and they published a book called ‘Design Patterns: Elements of Reusable Object-Oriented Software’. Design pattern is not a finished design to transform directly into the code, instead it is a specific piece of code (eg. template). You can follow the pattern details and implement a solution that suits to your program.

It was tested, proven, programming language independent(i.e no matter what language you use)strategy. They use OOP coding standard for solving a common problem. That means it represents an idea, not a particular implementation. By using this , you can make your code more flexible, reusable, and maintainable.

Patterns are usually confused with algorithms, because both describe typical solutions to some known problems. Algorithm always defines a clear set of actions that can achieve some goal, But a pattern is a more high-level description of a solution. The code of the same pattern applied to two different programs may be different.

Design patterns are divided into 3 categories. You can see there are 23 design patterns in the image below.

Design pattern categories
  1. Creational :- It is all about class instantiation or object creation. These patterns can be further categorized into Class-creational and object-creational patterns. Class-creational patterns use inheritance in the instantiation process, and object-creation patterns use delegation to get the job done.
  2. Structural :- It is about organizing different classes and objects to form larger structures and provide new functionality.
  3. Behavioral :- It is all about communication between Class’s objects.

We are going to cover singleton design pattern in this article.

What is Singleton Design pattern

It is a simplest pattern and gives a best way to create an object. It uses a single class to create an object. It creates one instance per container. Eg. In java, one instance per JVM. It allows other classes to use the object instance without instantiating the class.

Note :- Unit testing is difficult in singleton pattern. Because it does not use any instance variable and uses no reference to create.

We can use this pattern wherever it needs. Eg. Logger, Database, Configuration Manager, Error manager.

Eg. A developer wants to create a simple DBManager class to connect to a database and wants to access the database at multiple locations from the code, So We create DBManager class as a singleton class, so that only one instance of DBManager is created and a single connection is established.

public class DBManager{
private static volatile DBManager dbManager;

//Other classes cannot instantiate
private DBManager(){

//sometimes instance can be created by Reflection API.
if(dbManager != null){
throw new RuntimeException("use getDBManager method");
}
}


public static DBManager getDBManager(){

//Double checking singleton / double locking to make it threadsafe
if(dbManager == null){
synchronized(DBManager.class){
if(dbManager == null){
dbManager = new DBManager();
}
}
}
return dbManager;
}
}

This class can be used by other classes to get the instance.

UML diagram for a real world example.

Singleton example

You can get the Singleton pattern code from my GitHub account. It was implemented in java.

That’s all about singleton. Hope to cover other design patterns in upcoming articles.

Happy Coding.

--

--