Skip to content
We areTOP 1%

What is the Singleton pattern, and what are its drawbacks?

EasyConceptDesign Patterns
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

The Singleton pattern ensures that a class has exactly one instance and provides a global access point to it. It suits shared resources such as a configuration registry or a connection pool.

public class Config {
    private static final Config INSTANCE = new Config();
    private Config() {}
    public static Config getInstance() { return INSTANCE; }
}
  • Thread safety: eager initialisation as above, a static holder class or an enum are safe; lazy initialisation needs double-checked locking on a volatile field.
  • Drawbacks: it is global mutable state, hides dependencies, makes unit testing and mocking harder, and can become a contention point.
  • Prefer creating one instance at startup and passing it in through dependency injection.

More in Design Patterns

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account