Example analysis of python singleton pattern

  • 2020-05-07 19:56:34
  • OfStack

This article illustrates the python singleton pattern. Share with you for your reference. Specific analysis is as follows:

S 4en__ () is called before s 5en__ () to generate the instance object. The singleton pattern of design patterns can be implemented by taking advantage of the properties of this method and class. Singleton pattern is the creation of only 1 object, singleton pattern design class can only instantiate 1 object.  


class Singleton(object):
  __instance=None
  def__init__(self): 
    pass
  def__new__(cls,*args,**kwd):
    if Singleton.__instance is None: 
      Singleton.__instance=object.__new__(cls,*args,**kwd)
    return Singleton.__instance

The singleton pattern is a common software design pattern. It contains only one special class called a singleton class in its core structure. The singleton pattern can ensure that there is only one instance of each class in the system and that the instance is easy to be accessed by the outside world, so as to facilitate the control of the number of instances and save system resources. If you want only one object of a class to exist in the system, the singleton pattern is the best solution.

Obviously, the singleton pattern has three main points; 1 is that there can only be one instance of a class; 2 is that it must create the instance itself; 3 is that it must provide this instance to the entire system itself.

From a concrete implementation point of view, it is the following three points: 1 is the singleton pattern class only provides private constructor, 2 is the class definition contains a class of static private object, 3 is the class provides a static common function to create or get its own static private object.

In the object diagram below, there is one "singleton", and "customer a", "customer b", and "customer c" are the three customer objects of the singleton. As you can see, all client objects share 1 singleton object. And as you can see from the singleton's wiring to itself, the singleton holds a reference to itself.

Some resource managers are often designed as singletons.

In a computer system, the resources to be managed include software external resources, such as several printers per computer, but only one Printer Spooler, to avoid two print jobs being output to the printer at the same time. Each computer can have several fax CARDS, but only one piece of software should be used to manage the fax card to avoid having two fax jobs sent to the fax card at the same time. Each computer can have a number of communication ports, and the system should centrally manage these communication ports so that one communication port is not simultaneously invoked by two requests.

Resources to manage include software internal resources, for example, most software has one (or more) attribute (properties) file to store system configuration. Such a system should have one property file managed by one object.

The software internal resources to be managed also include parts that are responsible for recording the number of visitors to a website, parts that record internal events and error messages in the software system, or parts that check the performance of the system. These components must be centrally managed and not sold in many heads.

These resource manager artifacts must have only one instance, which is 1; They have to initialize themselves, so this is 2; Allow the entire system to access itself and this is its 3. Therefore, they all satisfy the condition of singleton pattern, which is the application of singleton pattern.

advantages:

1. Instance control

The singleton pattern prevents other objects from instantiating copies of their own singletons, thus ensuring that all objects access only 1 instance.

2. The flexibility

Because the class controls the instantiation process, the class has the flexibility to change the instantiation process.

Disadvantages of :

1. The overhead

This is a small number, but if you were to check for an instance of a class every time an object requests a reference, you would still have some overhead. You can solve this problem by using static initialization.

2. Possible development confusion

When using singletons (especially objects defined in a class library), developers must remember that they cannot instantiate objects using the new keyword. Because you may not have access to the library source code, application developers may find themselves unexpectedly unable to instantiate this class directly.

3. Object lifetime

The problem of deleting a single object cannot be solved. In a language that provides memory management (such as a language based on.NET Framework), only the singleton class can cause an instance to be unallocated because it contains a private reference to that instance. In some languages (such as C++), other classes can delete object instances, but this results in dangling references in singleton classes.

I hope this article is helpful for your Python programming.


Related articles: