Home » Singleton Design Pattern in Python

Singleton Design Pattern in Python

by Online Tutorials Library

Singleton Design Pattern in Python

Singleton design pattern is one of the Credential Design Pattern and it is easiest to implement. As the name describes itself, it is a way to provide one object of a particular type. It is used to describe the formation of a single instance of class while offering a single global access point to the object.

Singleton Design Pattern in Python

It prevents the creation of multiple objects of the single class. The newly created object will be shared globally in an application.

We can understand it with the simple example of Data connectivity. While setting up the database connection, we generate an exclusive Database connection object to work with the Database. We can perform every operation regarding database using that single connection object. This process is called a Single design pattern.

Motivation

Singleton design patterns are specially used in application types that need mechanisms over access to a mutual resource. As we have discussed earlier, a single class can be used to define a single instance.

One of the best benefits of using a singleton pattern is that we can restrict the shared resource and maintain integrity. It also prevents the overwriting in the current code by the other classes ensuing perilous or incompetent code. We can call the same object at multiple points of programs without worrying that it may be overwritten in the same points.

Implementation

To implement the singleton pattern, we use the static method. We create the getInstance() method that can return the shared resources. When we call the static method, either it gives the unique singleton object or an error singling an instantiated object’s existence.

It restricts to create the multiple objects of a defined class and maintain integrity.

We can take an example of the simple analogy – A county has a single central government that controls and accesses the country’s operation. No one can create another government in a certain period.

We can implement this analogy using the singleton pattern.

Example –

Output –

<__main__.GovtSingleton object at 0x0000026FDDAC8160>  <__main__.GovtSingleton object at 0x0000026FDDAC8160>  <__main__.GovtSingleton object at 0x0000026FDDAC8160>  File "C:/Users/DEVANSH SHARMA/PycharmProjects/MyPythonProject/pillow_image.py", line 119, in __init__      raise Exception("We cannot creat another class")  Exception: We cannot create another class  

Explanation –

In the above code, we have instantiated an object and stored it in a variable. We have also defined construction, which checks if there is another existing class; otherwise, it will raise an exception. We have then defined the static method named get_instance(), which returns the existing instance; if it is not available, then create it and return.

When we execute the script, the one GovInstance object is stored at a single point in the memory. When we create another object, it raises an exception.

Singleton Design Pattern in Python

Method – 2: Double Checked Locking Singleton Design Pattern

The synchronization of the threading is no longer in use because the object never is equal to the None. Let’s understand the following example.

Example –

Output –

X1 :  <__main__.A object at 0x00000262466480D0>  X2 :  <__main__.A object at 0x00000262466480D0>  Y1 :  <__main__.B object at 0x00000262465C2430>  Y2 :  <__main__.B object at 0x00000262465C2430>  

Advantages of Singleton Patterns

This pattern provides the following advantages.

  • A class created using the singleton pattern violates the Single Responsibility Principle, which means it can solve two problems simultaneously.
  • Single Pattern is difficult to implement in the multithreading environment because we need to ensure that the multithreading environment wouldn’t create singleton objects several times.
  • It makes the unit testing very hard because they follow the global state to an application.

Disadvantages of Single Pattern

Single Patterns also contain few demerits which are given below.

  • A class created using the singleton pattern violates the Single Responsibility Principle which means it can solve two problems at a single time.
  • Single Pattern is difficult to implement in the multithreading environment, because we need to ensure that multithreading environment wouldn’t create singleton object several times.
  • It makes the unit testing very hard because they follow the global state to an application.

Applicability of Design Pattern

We define the applicability of singleton design pattern as follows.

  • In the project, where we need a firm control over the global variables, we must use the Singleton Method.
  • Singleton patterns solves the most occurring problems such as logging, caching, thread pools, and configuration setting and often used in conjunction with the Factory design pattern.

You may also like