Singleton Design Pattern in PythonSingleton 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. 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. MotivationSingleton 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. ImplementationTo 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. 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 PatternsThis pattern provides the following advantages.
Disadvantages of Single PatternSingle Patterns also contain few demerits which are given below.
Applicability of Design PatternWe define the applicability of singleton design pattern as follows.
Next TopicFactory Design Pattern |