Why Singleton Pattern is considered as anti-design pattern ?

Sandesh Gaonkar
AIA Singapore Technology Blog
2 min readAug 15, 2021

We all have been using Singleton Pattern for a long time, but does it actually nominates to be a design pattern.

What is Singleton Pattern?

Singleton Pattern can be used when you need exactly one instance of the object. They are usually used for logging, database connections, caching.

How to write a Singleton Class?

1. Create a private constructor to restrict access from others.

2. Create a static method which will return the class instance.

Below is simple example of Singleton Class.

In software world everything comes with pros and cons. In case of Singleton Design pattern the list of cons is quite long.

Because of the long list of negative points Singleton Design pattern is also considered as anti-design pattern.

Lets talk about the disadvantages:

1. Violating Single Responsibility Principle :

Single Responsibility Principle states that every class should have a single task to do.

In case of Singleton class it will have two responsibility one to create an instance and other to do the actual task.

2. Breaks the Open Closed Principle :

Open Closed Principle can be explained in a single statement “Open for Extension closed for Modification”.

Singleton class always returns its own instance and is never open for extension.

3. Difficult to Test :

Singleton class have a global state for there instance. Programs that have global state hide there dependencies and makes it difficult to test.

4. Dependency Inversion Violation :

Dependency Inversion Principle ensures that change in low level details should not impact the high level abstraction.

Any low level changes in singleton pattern system we need to do change the Singleton class.

5. Tight Coupling :

There will be number of classes calling the Singleton class instance.

Any change in Singleton class will impact all other classes, this results into tight coupling.

Tight coupling is always harmful for system, any small change you can need to change all the classes implementing it, change all the test classes.

--

--