A singleton is a software design pattern that restricts the instantiation of a class to one single instance. Instantiation is the process of deriving an individual statement from a general one by replacing the variable with a name or other referring expression. In other words, a singleton is a class that allows a single instance of itself to be created, and it gives access to that created instance. This design pattern is useful when exactly one object is needed to coordinate actions across an entire software system. It’s used in programming languages such as Java and .NET.
What does the singleton pattern do?
The singleton pattern is one of 23 well-known design patterns (detailed below) that describe how to solve recurring problems in order to create objects that are easier to implement, change, test, and reuse. The key idea behind this pattern is to make the class itself responsible for controlling its instantiation and to ensure that it’s only instantiated once. The singleton design pattern works to answer questions such as:
- How can it be guaranteed that a class only has one instance?
- How can the single instance be easily accessed?
- How can a class control its instantiation?
- How can the number of instances be restricted?
- How can a global variable be accessed?
Design patterns
Design Patterns: Elements of Reusable Object-Oriented Software is a software engineering book that’s considered an important source for object-oriented design theory and practice. In it, 23 classic software design patterns are described under three umbrella categories: Creational, Structural, and Behavioral. The 23 design patterns are detailed below.
Creational
- Abstract factory
- Builder
- Factory
- Prototype
- Singleton
Structural
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Proxy
Behavioral
- Chain of responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template
- Visitor
Singleton pattern use cases
The singleton design pattern is a part of the creational patterns, which create objects rather than having to instantiate objects directly. This gives a program more flexibility in deciding which objects need to be created. Singletons are sometimes considered to be an alternative to global variables or static classes.
A global variable is a variable with global scope, meaning it’s visible throughout the program unless shadowed. They are created outside of a function and can be used by everyone, whether inside and outside of functions. The singleton design is often preferred to global variables because:
- Singleton instance fields don’t take up space in the global namespace
- Singletons permit lazy allocation and initialization whereas global variables will consume resources
A static class is a class that cannot be instantiated. A static class is similar to a class that is both abstract and sealed, and all members of the class are static in nature. The singleton design is preferred over static class because:
- Singletons can implement interfaces
- Singletons can be passed as parameters
- Singletons can have instances swapped out
- Singletons can be handled polymorphically, meaning multiple implementations can exist.
Implementing the Singleton pattern in JavaScript
Users can use the following object literal below to implement the Singleton design pattern in JavaScript:
var singleton = { method1: function () { //Write your usual code here }, method2: function () { //Write your usual code here } };