SOLID & Design Patterns Interview Question #33
What are the disadvantages of the Singleton Pattern?
Structural, Creational & Behavioral Patterns Junior Beginner
Quick Interview Answer
A global singleton can hide dependencies, introduce shared mutable state, make tests influence one another, and complicate concurrency. It can also tightly couple application code to a concrete global instance.
Detailed Explanation
A global singleton can hide dependencies, introduce shared mutable state, make tests influence one another, and complicate concurrency. It can also tightly couple application code to a concrete global instance.
If an application genuinely needs one service instance, registering that service with a DI container's singleton lifetime often preserves explicit constructor dependencies and improves testability.
Code Example
// Prefer explicit dependency:
public class AuditService
{
private readonly IClock _clock;
public AuditService(IClock clock)
=> _clock = clock;
}
// DI can control the required lifetime.