Skip to content
StackDecode
System Design
15. Software Design PatternsAnimated walkthrough

Singleton Pattern

One instance only, with a global access point.

The idea

Ensures a class has exactly one instance, such as a configuration or connection pool.

Watch it work

The steps light up one at a time. Use the controls to pause or step through.

  1. 1Make the constructor private
  2. 2Store the single instance
  3. 3Provide a getter
  4. 4Create lazily or eagerly
1/4

Step by step

  1. 1Make the constructor private
  2. 2Store the single instance
  3. 3Provide a getter
  4. 4Create lazily or eagerly

Where it is used

Shared config and pools.

Watch out for

Hidden global state makes testing hard.

Related