For years Singleton was sold to us as the most elegant way to manage single instances. Today it often becomes the first symptom of a serious technical debt.
If you have been coding for a while, Singleton is almost certainly one of the first patterns you learned. The first time explained it feels like a superpower: a clean approach to reach the same object from anywhere within the application.
The concept was born in 1994, in the legendary Gang of Four book. Its promise: simple and tempting. Guarantee for a single instance of a class and provide a global access point to it.
The technical trick looks brilliant. The class hides its constructor, keeps a reference to itself in a static field, and exposes a public method that returns it.
public class MySingleton {
private static MySingleton instance;
private MySingleton() {}
public static MySingleton getInstance() {
if (instance == null) {
instance = new MySingleton();
}
return instance;
}
}
In the early days of object orientation, it seemed like the perfect answer for centralized resources: configuration, logs, or database connections. For the best part of two decades, it was the undisputed hero of architecture.
The mirage of the single resource
Over time, the community began to question its foundations. The usual defense of Singleton is “I only want one instance”. That is the first misjudgment.
There is a huge difference between it being convenient to have a single object and it being a physical constraint of the domain.
The classic pattern is only justified when two instances would cause a catastrophic real-world failure. The textbook’s legitimate case is access to hardware that can’t be duplicated, such as a serial port or a print controller.
If two threads write at the same time to the same physical bus without a control point, the data is corrupted. Outside that scenario, using a Singleton for a database client or a configuration manager means accepting rigid coupling in exchange for a false sense of order.
The four cracks in the pattern
When the project grows, the initial elegance fades and four structural problems surface.
It is a disguised global variable. Singleton does not remove global state, it merely wraps it in a class and hides dependencies all over the code.
It wrecks tests. With the static call hardcoded, it is very hard to replace the real object with a mock, and the shared state makes one test contaminate another at random.
public class SalesService {
public void process() {
// Hidden, static dependency: impossible to simulate in an isolated test
DbConnection db = DbConnection.getInstance();
db.saveSale();
}
}
On top of that, it does not follow SOLID principles. The class takes on two jobs at once: its business logic and the management of its own life cycle. This is a clear violation of the Single Responsibility Principle.
Furthermore, it complicates concurrency. A naive lazy initialization lets two threads create two instances, so you must resort to double-checked locking, which penalizes performance.
public static MySingleton getInstance() {
if (instance == null) {
synchronized (MySingleton.class) {
if (instance == null) {
instance = new MySingleton();
}
}
}
return instance;
}
If you truly need one: Bloch’s enum
Sometimes the constraint is real, and you need a genuine Singleton. In that case, the approach Joshua Bloch recommends in Effective Java is not the one above, but a single element enum.
public enum Configuration {
INSTANCE;
private final String apiKey = loadKey();
public String getApiKey() {
return apiKey;
}
}
This approach solves for free what the classic Singleton complicates: it is thread-safe by construction, immune to reflection and correct on serialization. It is the best manual implementation when uniqueness is non-negotiable.
Dependency injection: declare instead of search
For everything else, modern architecture flipped the approach. The class should not fend for itself; it should only declare what it needs in its constructor.
public class PaymentProcessor {
private final ConfigurationProvider config;
public PaymentProcessor(ConfigurationProvider config) {
this.config = config;
}
public void process(double amount) {
System.out.println("Processing payment with key: " + config.getApiKey());
}
}
With this change, dependencies are visible, and testing becomes trivial again: just pass a mock when instantiating the class.
Singleton as a memory scope
Avoiding the pattern does not mean creating hundreds of uncontrolled copies in memory. That responsibility is delegated to the injection containers of Spring, NestJS or .NET.
In today’s software, the Singleton stopped being a hand-written pattern and became a memory management scope.
@Component // By default, the scope in Spring is SINGLETON
public class ConfigurationService implements ConfigurationProvider {
// Clean logic, no private constructors or static methods
}
At startup, the container does the heavy lifting. It scans the components, creates a single instance in its internal registry and injects it into whoever asks for it in the constructor.
One important nuance: that scope guarantees a single instance, but it does not protect its internal state against concurrency. That remains the responsibility of the design.
| Criterion | Classic Singleton | Singleton Scope (DI) |
|---|---|---|
| Instance control | The class itself (static) | The injection container |
| Coupling | High (hidden dependencies) | Low (constructor injection) |
| Unit testing | Very hard to isolate | Easy with mocks |
| SOLID principles | Violates single responsibility | Respects separation |
| Uniqueness scope | The whole JVM | The application context |
The classic Singleton was not a bad idea in 1994, it was the right answer for its time. But engineering has matured. The lesson is clear: pursue the efficiency of the single instance but never let a class become its own jailer.
