Fundamentals · Hard · Free
What is the Singleton pattern and why can it hurt testability?
FresherFundamentalsOOP & Language Fundamentals
Singleton trade-offs
Singleton ensures one instance—global access. Risk: hidden dependencies, hard to test, concurrency on lazy init, violates single responsibility.
- Prefer dependency injection container managing one instance.
- If used: enum singleton or static holder idiom for thread-safe lazy init.
- Global state complicates parallel tests.
Interview line: "I avoid singletons; I let Spring scope beans as singletons explicitly."