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.
- 1Make the constructor private
- 2Store the single instance
- 3Provide a getter
- 4Create lazily or eagerly
1/4
Step by step
- 1Make the constructor private
- 2Store the single instance
- 3Provide a getter
- 4Create lazily or eagerly
Where it is used
Shared config and pools.
Watch out for
Hidden global state makes testing hard.