All About Singleton Design Pattern

Navod Dilshan
Nerd For Tech
Published in
4 min readJan 3, 2023

--

What is a design pattern?

Design patterns are the pre-defined solution to commonly occurring problems in software designs. Design patterns are not the only solutions to problems. It’s a generalized description or template for how to address a problem.

What is Singleton design pattern?

As usual, the singleton design pattern is a type of creational design pattern. It lets you create only a single instance and gives you a single point of access to this instance from anywhere in the world.

Why Singleton design pattern?

It allows only one instance of a class to be created. But why do we need to allow creating a single instance from a class?

Usually, the singleton design pattern controls access to shared resources. The most common example is controlling access to the database. If the object is created from the database, and after that, the user tries to create another object from the same database, instead of creating a new object, the program returns the object already created.

This is what happens in the singleton design pattern.

Implementaion of Singleton design pattern?

Singleton design pattern has two major steps to follow.

01) Multiple objects are avoided through private static object creation in the class.

02) Give the public a method for creating an object.

These are the two steps to putting the singleton design pattern into action. So you can't understand what they really mean. So I will go through Java code snippets.

Database.java

public class Database {
private static Database connection;
private Database(){}
public static Database createConnection(){
if (connection==null){
connection= new Database();
System.out.println("New database connection created.");
}else {
System.out.println("Database connection is already created.");
}
return connection;
}
}

Client.java

public class Client {
public static void main(String[] args) {
Database.createConnection();
}
}

Important !

In this code, we convert the constructor to a private constructor. Have you ever wondered why we used it? Because private constructors are avoiding the creation of multiple objects using new keyword .

Is this code multithreaded?

No. This code is not valid for the multi-thread program. This code is not appropriate when the two programs run concurrently with two threads. So what is this solution for that? This is the Java code snippet for the solution.

Using the synchronized keyword, we can implement the singleton design pattern in multiple threads in this version(Thread-Safe version)

Databse.java

public class Database {
private static Database connection;

private Database() {

}

public static Database createConnection() {
synchronized (Database.class) {
if (connection == null) {
connection = new Database();
System.out.println("New database connection created!");
} else {
System.out.println("Database connection is already created");
}
}
return connection;
}
}

Client.java

public class Client implements Runnable {
public static void main(String[] args) {
Thread t1 = new Thread(new Client());
t1.start();
Thread t2 = new Thread(new Client());
t2.start();
Thread t3 = new Thread(new Client());
t3.start();
}

@Override
public void run() {
Database connection01 = Database.createConnection();
}
}

Is this the best practice for singleton design pattern ?

Absoultly not. Because the synchronized method is computationally very expensive, that blocks many executes pararlly and decrease the performance.So this version is not the better way to implement for thread safe singleton design pattern.

So, What is the better way?

Finally, you can get a better and optimized version to implement the singleton design pattern. That version is named as Doubly checked Locking method. The only difference between the previous and this version is that in this version, before executing the synchronized method, we check to see if an instance has been created. So the program will not execute that method every time.

Database.java

public class Database {
private static Database connection;

private Database() {

}

public static Database createConnection() {
if (connection == null) { // check before synchronized method
synchronized (Database.class) {
if (connection == null) {
connection = new Database();
System.out.println("New database connection created!");
} else {
System.out.println("Database connection is already created");
}
}
} else {
System.out.println("Database connection is already created");
}

return connection;
}
}

This is all about Singleton design pattern, and I really hope you learned anything new here. I’ll see you soon in the next blog. Happy coding!!!

--

--