Python Singleton mode instance analysis and python instance analysis
This article describes the python Singleton mode. Share it with you for your reference. The specific analysis is as follows:
_ New _ () is called before _ init _ () to generate instance objects. The Singleton mode of design mode can be implemented by using this method and the characteristics of class attributes. The Singleton mode is used to create a unique object. The Singleton mode design class can only instantiate one 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 mode is a common software design mode. Its core structure only contains a special class called Singleton class. The Singleton mode ensures that there is only one instance in a class in the system and the instance is easy to access, so as to conveniently control the number of instances and save system resources. If you want to have only one class object in the system, the singleton mode is the best solution.
Obviously, the singleton mode has three key points. One is that a class can only have one instance; the other is that it must create the instance on its own; and the third is that it must provide the instance to the entire system on its own.
From a specific implementation perspective, there are three points: first, the class in singleton mode only provides private constructors, and second, the class definition contains a static private object of this class, third, this class provides a static common function for creating or obtaining its own static private object.
In the following object diagram, there is a "singleton object", and "Customer A", "Customer B", and "customer C" are the three customer objects of the singleton object. As you can see, all customer objects share one singleton object. In addition, we can see from the singleton object to its own connection line that the singleton object holds a reference to itself.
Some resource managers are often designed to work in singleton mode.
In computer systems, resources to be managed include external software resources. For example, each computer may have several printers, but only one Printer Spooler can be used, this prevents two print jobs from being output to the printer at the same time. Each computer may have several HbA cards, but only one software should be responsible for managing the HBA card to avoid the situation where two fax jobs are simultaneously transferred to the HBA card. Each computer can have several communication ports. The system should centrally manage these communication ports to prevent a communication port from being simultaneously called by both requests.
Resources to be managed include internal software resources. For example, most software has one or more properties files to store system configurations. Such a system should manage an attribute file by an object.
Internal software resources to be managed include components that are responsible for recording the number of visitors to the website, components that record internal events of the software system, components with error information, or components that check the system performance. These components must be centrally managed and cannot be managed by multiple leaders.
These Resource Manager components must have only one instance, which is one; they must be self-initialized; this is second; allow the entire system to access itself this is third. Therefore, they all meet the singleton mode conditions and are the application of Singleton mode.
Advantages:
I. instance Control
The Singleton mode prevents other objects from instantiating copies of their own Singleton objects, so that all objects can access a unique instance.
Ii. Flexibility
Because classes control the instantiation process, classes can flexibly change the instantiation process.
Disadvantages:
I. overhead
Although the number is small, if you want to check whether there is a class instance for each object request reference, you still need some overhead. You can solve this problem by using static initialization.
Ii. Possible development Obfuscation
When using a singleton object (especially an object defined in a class library), developers must remember that they cannot use the new keyword to instantiate an object. Because the source code of the library may not be accessible, application developers may accidentally find that they cannot directly instantiate this class.
Iii. Object lifetime
It cannot solve the problem of deleting a single object. In the language that provides memory management (for example, the. NET Framework-based language), only the singleton class can cause the instance to be unallocated because it contains private references to the instance. In some languages (such as C ++), other classes can delete object instances, but this will lead to floating references in the singleton class.
I hope this article will help you with Python programming.