Java Zero to All All questionsPractise free
Interview questions › Design Patterns in Java › Creational Design Patterns

Walk me through implementing a thread-safe Singleton, and why is the GoF Singleton often called an anti-pattern?

Level III · MidCreational Design Patterns
Short answer

I'd reach for the enum singleton or the lazy-holder idiom rather than hand-rolling double-checked locking, because the holder gets laziness and thread-safety free via the JVM's class-init guarantee and enum is additionally immune to reflection and serialization attacks.

Explanation

The anti-pattern critique is really about the static getInstance() global access point: it is hidden global mutable state that couples callers invisibly, leaks between tests, and resists mocking. The clean modern answer is to let a DI container own the single instance (Spring's default singleton scope) so you keep one-instance economy but inject it, preserving testability.

Common mistakes
  • Writing double-checked locking without volatile.
  • Not mentioning the enum singleton or holder idiom.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Creational Design Patterns.

Practise free

More on Creational Design Patterns