
Singleton
A singleton is a design pattern from software development that ensures exactly one single instance of a particular class — i.e., a blueprint for objects in a program — exists. It solves the problem that certain resources in a program may only exist once, such as a database connection or a central settings object.
In software development there are situations in which a particular object — meaning a concrete thing that a program creates from a blueprint — must exist exactly once. A singleton is a design pattern that technically enforces this. Design patterns are proven solution templates for recurring problems in programming. A singleton ensures the following: no matter how often a program tries to create this object, it receives the same one back every time. There simply is no second copy.
Why a program sometimes needs only one
Some resources in a program are inherently singular by nature. A connection to a database, for instance, is costly to set up and should be managed centrally. If every part of the program independently established a new connection, dozens of parallel connections would quickly arise — unnecessary, costly, and hard to control.
Another typical example is a configuration object that holds an application’s settings. If two different parts of the program each create their own copy and modify it differently, the program eventually loses track of which settings are supposed to apply. The singleton prevents this contradiction by guaranteeing a single instance binding for everyone.
The trick behind the uniqueness
A singleton achieves its uniqueness through two technical tricks. First, the normal way of creating an object — the so-called constructor — is locked from the outside. No one can simply create the object anew using the usual command. Second, there is a single public method, often called “getInstance,” that delivers the object.
When called, this method checks: Does the object already exist? If so, it returns the existing instance. If not, it creates the object — exactly this one time — and remembers it internally. On the next call, it returns the same one again. The mechanism works like a doorman: he lets only a single visitor in and sends every further one to the same room instead of opening a new one.
In environments where multiple parts of a program run simultaneously — so-called multithreading — the singleton becomes more complicated. Two parts could check at the same time whether the instance exists, and both could create a new one simultaneously. That’s why a robust singleton needs additional safeguards there to control this moment of simultaneous access.
Where the singleton shows up in practice
You encounter singletons in almost every larger software application, even if you don’t see them from the outside. Logging systems — programs that record error messages and events — are often built as singletons. There should be one unified log file, not one per program part.
In games, a central game state is often managed as a singleton, handling the current score, the active level, or the state of the player character. The pattern also appears in mobile apps: the central data store that shares user data between different screens of an app often works on the singleton principle.
In discussions about software architecture, however, the singleton is considered a controversial pattern. Critics call it hidden global state: every part of the program can access the same object at any time, without this being immediately apparent from the code. This makes programs harder to test and harder to understand. That’s why many developers today recommend using the singleton only where uniqueness is truly and strictly necessary — and not simply because it is convenient.