Stack Explorer

Singleton

creational

Guarantees a single instance of a class

Type: pattern

Principles

Single instanceGlobal access pointLazy initialization possible

When to use

When you really need a single instance (logging, config)

When not to use

Almost always - prefer DI

Pros and Cons

Ventajas

  • + Single instance control
  • + Lazy initialization
  • + Global access point

Desventajas

  • - Difficult to test
  • - Global state (anti-pattern)
  • - Hides dependencies

Related Technologies

Related Patterns