Python – The Singleton Design Pattern


Understanding Singleton design pattern

The intent of singleton design pattern is to have only one instance of a class and it should be accessible to multiple clients through well defined access point. Singleton are typically used in cases such as loggin or database operations and many other where there is need to have only one instance that is available across the application. This allows to to avoid conflicting request on the same resource.
Implementing a classical Singleton in Python
Let us implement a classical Singleton pattern in python. In this example we will do the following things :
  •  Creation of only one instance of the singleton class.
  • if an instance exists, we will again provide the same object.

code description:

The new method in the code is a special method of python to instantiate objects which is used to control the objects creation. The object gets created with the new method , but before creating the object, it checks whether the object already exists. The hasattr method is a special method in python to know if an object has a certain property. It is used to see if the class object has the instance property, which checks whether the class already has an object.Here in our code we have requested s object before sA object. When s object is requested the hasattr() detects that object does not exists and hence s allocates the new object instance. When s1 object is requested,hasattr()  detects that objet already exists and hence s1 allocates the existing object instance (located at 0x000002345C7A5B48).

code description: In the example , we define the variable that will hold the single object to be instantiated. A class Singleton is defined. When we request s = Singleton() , it calls the init method. Our constructor checks whether there is an existing class. In the initial calling of the  Singleton() , there is no Instance created so it results to the print statement “init method is called.”. Then Singleton.getInstance() is called, Object is created here. Now when we call the Singleton() class, as the instance is created, the init method results to the print statement “Instance is already created :”, self.getInstance()”. self.getInstance() gives the existing object instance “
Code Description: The call method gets called when an object needs to be created for an already existing class. In this code we instantiate the int class with int(20,40), then the call method of the ClassA metaclass gets called. Now the metaclass now controls the instantiation of the object.

A Real-world scenario - the singleton pattern

This is a database application to show the use of Singletons. Lets us consider an example of cloud service that involves multiple read and write operation in the database. The complete cloud service is split across multiple services that performs database operations.An action in the UI internally will call an API, which results in DB operation.

The database is shared across different service. So in order to design the cloud service better, the following points must be managed.

  • The Consistancy should be maintained across the operations in the database.

  • Memory and CPU utilization should be optimal for the handling of multiple operation on the database.

We will go through factory design pattern in the next topic of design patterns in python. 

View the code as well as output in the github source:


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *