Singleton Pattern (Software Design)
A is a creational design pattern that ensures a class has only one instance and provides a global access point to that instance. It is commonly used for managing shared resources like database connections, configuration settings, or logging services, where having multiple instances could lead to inconsistencies or resource conflicts. The pattern achieves this by making the constructor private and providing a static method (e.g., getInstance()) that controls access to the single instance.
Key Features:
Single Instance: Only one object of the class can exist throughout the application lifecycle.
Global Access: The instance can be accessed from anywhere in the code via a static method.
Lazy Initialization: The instance is created only when first requested, improving performance.
Thread Safety: Proper implementations handle concurrent access to prevent multiple instances in multi-threaded environments.
Common Use Cases:
Database connection pools.
Configuration managers.
Logging services.
Caching systems.
Print spoolers in operating systems.
Drawbacks:
Violates the Single Responsibility Principle by combining instance control with business logic.
Can make unit testing difficult due to global state and difficulty in mocking.
May mask poor design, such as excessive coupling between components.
💡 Note: While widely used, many experts caution against overusing the Singleton pattern, favoring dependency injection or other patterns for better testability and flexibility.
The Singleton (Whisky)
The Singleton is an award-winning Single Malt Scotch Whisky produced by Diageo, crafted from three Highland distilleries: Glen Ord, Glendullan, and Dufftown. Known for its smooth texture and complex flavor profile—featuring notes of citrus, vanilla, honey, and spice—it undergoes a slow fermentation (75 hours) and gentle distillation, followed by maturation in American and European oak casks.
Alex Singleton (NFL Player)
Alex Singleton is a professional football linebacker who re-signed with the Denver Broncos in March 2026 on a two-year, $15.5 million contract. A key defensive leader, he led the team in tackles in 2025 despite battling testicular cancer and missing only one game. He is expected to anchor the middle of the Broncos’ defense alongside fellow linebacker Justin Strnad.
Singleton (Mathematics)
In mathematics, a singleton refers to a set containing exactly one element. For example, the set {5} is a singleton. This concept is foundational in set theory and logic, where it helps define uniqueness and identity. The term inspired the software design pattern’s name, emphasizing the idea of "one and only one."
Factsheet
language agnostic - Purpose of singletons in programming - Stack Overflow
What, when why Singleton design pattern?
Singleton Design Pattern: Managing Global States in Your Applications
Static singleton vs environment object? : iOSProgramming
Videos
The main advantage of a singleton over a class consisting of statics is that you can later easily decide that you need in fact more than one instance, e.g. one per thread.
However, in practice the main purpose of singletons is to make people feel less bad about having global variables.
A practical example for a good use of a singleton: you have an app that uses an SQL database and you need a connection pool. The purpose of such a pool is to reuse DB connection, so you definitely want all clients to use the same pool. Thus, having it as a singleton is the correct design. But one day you need the app to connect to a second DB server, and realize that you cannot have connections to different servers in the same pool. Thus your "one instance overall" singleton becomes "one instance per DB server".
why you would wan't to
I wouldn't because singletons usually are a bad way to solve your problems. My recommendation to you is to avoid them completely.
The main reasons are:
- Singletons mostly represent global state (which is evil).
- Correct dependency injection becomes impossible.
I suggest you read the rest (including thorough explanations) in this Google employee's blog:
- Singletons are Pathological Liars
- Where Have All the Singletons Gone?
- Root Cause of Singletons
- Flaw: Brittle Global State & Singletons
I am reading about Singleton design pattern and I don't understand why we should use it, a lot of people will say, oh to create a single instance of that class in order to save resources, and it's fine I get it, then we should use it for every class? Because from my understating we use it only on classes that their data not change, se they upload it one time in the memory and share them across the code, am I right? At this point will be useless to instantiate multiple objects which return the same unchangeable data? Right?