Singleton Design Pattern

Kerim Embel
3 min readDec 7, 2021

Singleton Pattern is one of the Creative Design Patterns.

It is probably the most commonly used Design Model because of the problems it solves and the ease of implementation.

The main concept of Singleton Design Pattern is to create only one instance.

Yeah, I’ve heard that before, but what does it mean?

This means that the creation of the class is done inside the class. Other classes that want to access the class, access the instance created within the class.

In order to do this, a private constructor is created, so it is ensured that our class cannot be created over another class.

A static member of the same type as our class is created and a static method is created to reach this member.

When the mentioned things are put into code, the result is as follows.

public class SingletonLogger {

private static SingletonLogger logger;

private SingletonLogger() {}

public static SingletonLogger getLogger() {
if(logger == null) {
logger = new SingletonLogger();
}
return logger;
}

public void info(String message) {
System.out.println(GREEN + message + GREEN);
}

public void error(String message) {
System.out.println(RED + message + RED);
}
}

As seen in the example above, a static member of the same type as our class has been created inside the class.

Note that the logger object is created inside the getLogger() method, not when the object is introduced. This way of defining is known as Lazy Initialization and the object is not created until it is needed.

The direct creation of the object when introduced is known as Eager Initialization. Nothing changes as output, but using Lazy Initialization avoids creating unnecessary objects in a multi-thread environment, thus preventing unnecessary objects from taking up memory space.

Now let’s try it!

public class Main {

public static void main(String[] args){

SingletonLogger LOGGER1 = SingletonLogger.getLogger();
SingletonLogger LOGGER2 = SingletonLogger.getLogger();

System.out.println("LOGGER1 -> " + LOGGER1);
System.out.println("LOGGER2 -> " + LOGGER2);

if(LOGGER1==LOGGER2){
LOGGER1.info("Singleton works!");
} else {
LOGGER1.error("Singleton doesn't works :/");
}
}
}

The ‘new’ keyword cannot be used to access the SingletonLogger class, as seen in the Main method. Instead, the getLogger() method, which is the static method of the SingletonLogger class, is called.

When the method is run, the output will be as follows.

Output of Main method

As can be seen in the output, the memory addresses of the 2 objects created from the SingletonLogger class are the same, so they are the same object!

No matter how nicely the example works, in real life scenario Singleton objects are not used like this because it is not Thread Safe that way. In a multi-thread environment, it will be a problem if multiple threads call the getLogger() method at the same time.

I will explain how to make this class Thread Safe in another article.

Click here to see the example in this article.

See you until the next one!

--

--