Creational patterns
Singleton
Exactly one instance with a shared access point – use sparingly, it is global state in disguise.
Article
Singleton – one instance, and the reasons to hand it to a DI container
Singleton guarantees that a class has exactly one instance available to the whole application. We show thread-safe implementations in five languages and explain why today it is usually better to register the object as a singleton in a DI container.
Related articles
- Flyweight – how to fit 60,000 stadium seats in memory
Flyweight separates the state shared by many objects from the state unique to each one, and shares the former. A stadium seat map example from a ticket booking system in C#, TypeScript, Java, Go and…
- Abstract Factory – whole families of objects that fit together
Abstract Factory creates a complete set of related objects from one family and makes sure nobody mixes parts from different sets. An example with two payment provider integrations.
Around the topic
- Singleton · Design Patterns Revisited · Game Programming Patterns article · web · Robert Nystrom
- Singleton Pattern – Design Patterns (ep 6) video · youtube · Christopher Okhravi
- Using Scoped Services From Singletons in ASP.NET Core article · web · Milan Jovanović
- Inversion of Control Containers and the Dependency Injection pattern article · web · Martin Fowler
- Effective Java book · openlibrary · Joshua Bloch
- Singleton article · web · Refactoring.Guru
- Singleton / Singleton article · web · Refactoring.Guru
- Mastering C# Design Patterns: Practical Examples of Singleton, Factory, and Observer article · devto · Leandro Veiga
- UnityCommunity/UnitySingleton repo · github · UnityCommunity