Singleton Design Pattern in Python
Singleton design pattern is a pattern which we use to restrict a class to initialize its various objects.
In short, it can be said as a method to define a class, defined in a way that it allows only one object to be created and executed in the complete program execution.
Classes defined with singleton design pattern cannot have multiple objects. It is the simplest design pattern, most simple to design but most debatable in the complexity of its use perspective.
If we need to achieve this without any worry than we (coder) need to keep the control of instance allocation and the user should not be allowed to control it. An easy way to do that is by making class private with a single instance. In this way, it provides only one instance of a particular kind.
Image :
This diagram shows the overall working
Code in Python for Singleton Design Pattern
There are two commonly used methods to implement the Singleton design pattern in Python.
- Monostate Singleton Design Pattern
- Classic Singleton Design Pattern
We will discuss one by one.
1. MONOSTATE Singleton Design Pattern
It is also known as Brog’s pattern. In this, we create multiple instances but they all share the same condition(state). Our main focus is on the sharing of the state rather than the objects.
Below is the Python code for this:
class Single_Borg: # state shared by each instance __shared_state = dict() # constructor method def __init__(self): self.__dict__ = self.__shared_state self.condition= 'CodeSpeedy' def __str__(self): return self.condition # Driver code if __name__ == "__main__": #creating objects computer1 = Single_Borg() computer2 = Single_Borg() computer3 = Single_Borg() computer1.condition = 'Python Programs' # computer1 changed the state computer2.condition = 'Solution' # computer2 changed the state print(computer1) print(computer2) computer3.condition = 'codes' # computer3 changed the shared state print(computer1) print(computer2) print(computer3)
Output
Solution Solution codes codes codes [Program finished] 
2. The CLASSIC implementation of the Singleton Design Pattern
In this, we create getinstance method by the help of a static method in the class. Here the constructor is kept private as if there comes a need to raise an exception. The getinstance method returns the shared item
class Singletonclass: __case = None @staticmethod def getoccurence(): #Static access method if Singletonclass.__case == None: Singletonclass() return Singletonclass.__case def __init__(self): #Virtual Private constructor if Singletonclass.__case != None: raise Exception("Example of class singleton!") else: Singletonclass.__case = self x = Singletonclass() print (x) x = Singletonclass.getoccurence() print (x) x = Singletonclass.getoccurence() print (x)
Output
<__main__.Singletonclass object at 0x7d7a290b00> <__main__.Singletonclass object at 0x7d7a290b00> <__main__.Singletonclass object at 0x7d7a290b00> [Program finished]
I hope I was able to make you understand the code. Try to implement it and if you have any doubt or error leave a comment. Your feedback will be appreciated
Leave a Reply