Prerequisite: Singleton Design pattern | Inroduction
Singleton Method is a type of Creational Design pattern and is one of the simplest design pattern available to us. It is a way to provide one and only one object of a particular type. It involves only one class to create methods and specify the objects.
Singleton Design Pattern can be understood by a very simple example of Database connectivity. When each object creates a unique Database Connection to the Database, it will highly affect the cost and expenses of the project. So, it is always better to make a single connection rather than making extra irrelevant connections which can be easily done by Singleton Design Pattern.
Definition: The singleton pattern is a design pattern that restricts the instantiation of a class to one object.
Now let’s have a look at the different implementations of the Singleton Design pattern.
Method1 : Monostate/Borg Singleton Design pattern
Singleton behavior can be implemented by Borg’s pattern but instead of having only one instance of the class there are multiple instances that share the same state. Here we don’t focus on the sharing of the instance identity instead we focus on the sharing state.
Algorithms Algorithms Geeks Geeks Geeks
Double Checked Locking Singleton Design pattern
It is easy to notice that once an object is created, the synchronization of the threading is no longer useful because now object will never be equal to None and any sequence of operations will lead to consistent results.
So, when the object will be equal to None, then only we will acquire the Lock on the getInstance method.
A1 : __main__.X object at 0x02EA2590 A2 : __main__.X object at 0x02EA2590 B1 : __main__.Y object at 0x02EA25B0 B2 : __main__.Y object at 0x02EA25B0
Classic implementation of Singleton Design Pattern
In the classic implementation of the Singleton Design pattern, we simply use the static method for creating the getInstance method which has the ability to return the shared resource. We also make use of so-called Virtual private Constructor to raise the exception against it although which is not much required.
__main__.Singleton object at 0x014FFE90 __main__.Singleton object at 0x014FFE90
Class Diagram of Singleton Design Pattern
Advantages of using Singleton Method:
- Initializations: Object created by Singleton method is initialized only when it is requested for the first time.
- Access to the object: We got the global access to the instance of the object.
- Count of instances: In singleton method classes can’t have more than one instance
Disadvantages of using Singleton Method:
- Multithread Environment: Its not easy to use the singleton method in multithread environment, because we have to take care that multithread wouldn’t create singleton object several times.
- Single responsibility Principle: As the Singleton method is solving two problems at a single time, it doesn’t follow the single responsibility principle.
- Unit testing process: As they introduce the global state to the application, it makes the unit testing very hard.
- Controlling over global variables: In the projects where we specifically need the strong control over the global variables, it is highy recommended to use Singleton Method
- Daily Developers use: Singleton patterns are generally used in providing the logging, caching, thread pools and configuration settings and oftenly used in conjuction with Factory design pattern.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.