What is the main criticism of the Singleton pattern?
Options
- It uses too much memory for the single object that it keeps
- It adds hidden global state, so testing and parallelism get harder (Correct option)
- It cannot be implemented in some languages, such as Python
- It is always slower than a normal class in every case
Answer
It introduces hidden global state and makes testing and parallelism harder
Why
A Singleton guarantees there is only one instance of a class and lets everyone grab it from anywhere.
That is basically a global variable: code depends on it silently, tests share its state and interfere with each other, and it is hard to replace with a fake.
Prefer creating one instance at startup and passing it to whoever needs it (dependency injection).
Remember
Singleton = global state in disguise. Prefer injecting the instance.