mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 02:26:40 +00:00
39 lines
2.2 KiB
Markdown
39 lines
2.2 KiB
Markdown
# Singleton
|
||
|
||
Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code.
|
||
|
||
Singleton has almost the same pros and cons as global variables. Although they’re super-handy, they break the modularity of your code.
|
||
|
||
You can’t just use a class that depends on Singleton in some other context. You’ll have to carry the Singleton class as well. Most of the time, this limitation comes up during the creation of unit tests.
|
||
|
||
## Summary
|
||
|
||
Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.
|
||
|
||
## Problem
|
||
|
||
The Singleton pattern solves two problems at the same time, violating the Single Responsibility Principle:
|
||
|
||
1. Ensure that a class has just a single instance.
|
||
|
||
2. Provide a global access point to that instance.
|
||
|
||
## Solution
|
||
|
||
All implementations of the Singleton have these two steps in common:
|
||
|
||
- Make the default constructor private, to prevent other objects from using the new operator with the Singleton class.
|
||
- Create a static creation method that acts as a constructor. Under the hood, this method calls the private constructor to create an object and saves it in a static field. All following calls to this method return the cached object.
|
||
If your code has access to the Singleton class, then it’s able to call the Singleton’s static method. So whenever that method is called, the same object is always returned.
|
||
|
||
## How to Implement
|
||
|
||
1. Add a private static field to the class for storing the singleton instance.
|
||
|
||
1. Declare a public static creation method for getting the singleton instance.
|
||
|
||
1. Implement “lazy initialization” inside the static method. It should create a new object on its first call and put it into the static field. The method should always return that instance on all subsequent calls.
|
||
|
||
1. Make the constructor of the class private. The static method of the class will still be able to call the constructor, but not the other objects.
|
||
|
||
1. Go over the client code and replace all direct calls to the singleton’s constructor with calls to its static creation method.
|