Singleton pattern
The singleton pattern is categorized under creational design patterns. This pattern restricts the instantiation of specific classes, ensuring that only one unique instance of the object can exist. Essentially, it functions as a global variable accessible from any part of the program.
Consider a scenario with a dog as a Singleton. When we refer to this Singleton, we are consistently pointing to the same instance. Any changes made, such as incrementing the dog's age in one module, will reflect universally across all instances where the dog Singleton is employed.
This singleton dog can be globally shared across multiple modules. When modules import the singleton object, they all reference the same instance. A single method, birthday()
, is responsible for modifying the age property. Each time any module invokes the birthday()
method, the age increases by one, and this change becomes visible throughout all modules utilizing the singleton.
It's important to note that creating a singleton doesn't necessarily require the use of es6 class
syntax; a simple object literal suffices and works equally well.
The concept of a singleton is frequently employed in our applications, often unintentionally. When an object literal is exported from one module, it essentially becomes a singleton. If another module imports this object and makes modifications to its properties, these changes will have a widespread impact throughout the entire program.
To safeguard against unintentional property mutations, consider using Object.freeze
:
Common Use Cases
Why opt for a single instance of an object?
The primary rationale is to regulate access to a shared resource, such as a database or file, to avoid potential issues. Having multiple instances connected to a database, for instance, could lead to chaos. Instead, employing a singleton ensures only one instance exists within our database.
This ensures a single, controlled instance of the DbConnection
class with a designated URI for our PostgreSQL database.
Singletons in the Real World
Consider the analogy of a person having only one father. It's an inherent singleton scenario; an individual can't have multiple fathers. From this perspective, a father is essentially a singleton.
| Pros | Cons | | :--------------------------------- | :------------------------------------------------------------------------------- | | Single Instance, Memory Efficiency | Solves Multiple Concerns, Potentially Violates Single Responsibility Principle | | Global Accessibility | Increased Coupling, Components Know Too Much About Each Other |
Summary
-
Memory Efficiency: Singleton patterns contribute to memory optimization by allowing only one instance. This means memory is allocated once and persists throughout the entire program, preventing unnecessary duplication.
-
Default Singletons (Modules): Since ES2015, modules inherently function as singletons. There's often no need to explicitly create singletons, as the language incorporates this feature.
-
Exercise Caution in Value Modification: Altering a value in a singleton can have widespread consequences. Changes propagate across all program modules using the singleton, potentially leading to unexpected behaviors.
-
Testing Challenges: Singleton patterns can pose challenges in testing. With only one allowed instance, state changes in the singleton may impact tests, leading to failures.
-
Global Variable Analogy: Singletons are akin to global variables, accessible throughout the entire program. Care must be exercised to avoid unintended overwrites of values, as such changes can have a broad impact on the entire program.