The Singleton Design Pattern
The Singleton pattern ensures that a class has only one instance and provides a global point to access it.
Use the Singleton pattern when a class should only have a single instance throughout the entire application, typically for logging, driver objects, caching, thread pools, or database connections.
- TypeCreational
- Time ComplexityO(1) Constant Time – The algorithm runs in constant time, irrespective of the input size. Operations like accessing an array element fall into this category.
- Efficiency8
-
Learning Effort
- Average3
- Conceptual3
- Debug and Maintain4
- Implementation2
- Prerequisites2
- Versatility3
Things you should know before you start
- Class Understanding of object-oriented programming concepts, specifically classes.
- Static Methods Understanding of static methods and their role in object-oriented programming.
Minimal Example
Managing a single database connection throughout the application.
class Database { private static instance: Database; private constructor() {} public static getInstance(): Database { if (!Database.instance) { Database.instance = new Database(); } return Database.instance; } } const db1 = Database.getInstance(); const db2 = Database.getInstance(); console.log(db1 === db2); // Output: true
What’s happening in thie example
In this example, the Database class is a Singleton. We use the `getInstance` method to get the single instance of the Database class. The `clientCode` demonstrates that `db1` and `db2` are indeed the same instance.
How the Singleton Works
The Singleton pattern restricts a class from instantiating multiple objects. It is used where only a single instance of a class is required to control actions.
This pattern is particularly useful when one object controls access to a resource, such as a file or a network connection. By funneling access through a single point, we can synchronize multiple threads for object access.
Entities in the Singleton Design Pattern
-
SingletonThe Singleton class defines the `getInstance` method that serves as an accessor for clients and a single point of instance creation.
- Singleton has a one-to-one instantiates relationship with Singleton
What to watch out for when using or considering this design pattern.
Avoid using the Singleton pattern when you need multiple instances of a class, as it restricts a class to a single instance. Overuse of this pattern can lead to problems in parallel testing and make the system tightly coupled.
Design patterns often confused with the Singleton Design Pattern
The following design patterns are often confused with the Singleton
for various resons.
If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.
-
Factory Method – The Factory Method pattern provides an interface for creating instances of a class, with its subclass deciding which class to instantiate. It abstracts the process of object creation.
The Factory Method pattern is similar to the Singleton in the following ways.
- Both are creational design patterns.
- Both provide a way to control and manage object creation.
- Factory Method allows creating instances of multiple classes, while Singleton restricts to a single instance of one class.
- Factory Method focuses on object creation through inheritance and polymorphism, while Singleton focuses on a single object instance for the entire application.
-
Prototype – The Prototype pattern creates new instances by copying an existing instance, known as the ‘prototype’. This is used when the cost of creating an object is more expensive or complex than copying an existing one.
The Prototype pattern is similar to the Singleton in the following ways.
- Both are creational design patterns.
- Both deal with object creation mechanisms.
- Prototype creates new instances through cloning, while Singleton restricts instance creation to one.
- Prototype is often used when object initialization is costly, whereas Singleton is used to restrict instance creation.