Skip to content
Preptima
mediumConceptCodingDesignMidSenior

How would you implement a singleton, and why do many teams treat it as an anti-pattern?

4 min readUpdated 2026-08-15

Answer

Short answer

The mechanics are easy - an enum in Java, a module-level value elsewhere - and are not the interesting part. The objection is that a singleton bundles two unrelated decisions, one instance and globally reachable, and the second is what makes dependencies invisible and tests order-dependent.

Was this answer helpful?

Likely follow-ups

  • Why is double-checked locking broken without a memory barrier, and what fixes it?
  • Your singleton reads configuration at class-initialisation time. What breaks in a test suite?
  • How is a Spring singleton bean different from a classic singleton?
  • When is a genuinely global single instance the right answer?

Related questions

Related Topics