Singleton mode (Singleton pattern)
The singleton mode is primarily used to ensure that only one instance of a class exists, such as the admin in Django, where all apps are registered in the same admin.
For example, the configuration information for a server program is stored in a file, and the client reads the configuration file information through a AppConfig class. If the content of the configuration file needs to be used in many places during the run of the program, that is , many places need to create an instance of the AppConfig object, which leads to the existence of multiple AppConfig instance objects in the system, which can seriously waste memory resources . Especially if the configuration file has a lot of content. In fact, a class like AppConfig, we want only one instance object to exist during the run of the program.
In Python, there are several ways to implement a singleton pattern:
1. Using the module
2. Using __new__
3. Using adorners
4. Using meta-classes
Using modules
the Python module is a natural singleton mode , because when the module is first imported, the file is generated .pyc , and when the second import, the file is loaded directly .pyc without executing the module code again. Therefore, we just need to define the relevant functions and data in a module, we can get a singleton object. If we really want a singleton class, consider doing this:
# Singleton.pyclass WuKong (object): def foo (self): Passsun = WuKong ()
From singleton import Sunsun.foo ()
Using the __new__ method
We use a variable to correlate the return value of new, so long as the first instantiation is _instance after the value has been returned, the object that was instantiated for the first time.
1 class Singleton (object): 2 _instance = None 3 4 def __new__ (CLS, *args, **kwargs): 5 if cls._instance is None: 6 cls._instance = Super (Singleton, CLS). __new__ (CLS, *args, **kwargs) 7 return cls._instance
A == Singleton ()print is b)----------------------------True
Using adorners:
This way is similar to the one in the above, there is a variable if the variable is not empty, or the class is not in this variable then I created, the second time the creation of the variable has a value, so directly return
fromFunctoolsImportWrapsdefSingleton (CLS): Instance={} @wraps (CLS)defgetinstance (*args, * *Kwargs):ifCls not inchInstance:instance[cls]= CLS (*args, * *Kwargs)returnInstance[cls]returnGetinstance@singletonclassMyclass (object):deffoo (self):Pass
The above warps do things: If you do not add direct printing myclass.__name__ value is getinstance, add the later is MyClass
A == Myclass ()print is b)---------------------True
Using meta-classes
classSingleton (type): _instances= {} def __call__(CLS, *args, * *Kwargs):ifCls not inchCls._instances:cls._instances[cls]= Super (Singleton, CLS).__call__(*args, * *Kwargs)returnCls._instances[cls]#Python2classMyClass (object):__metaclass__=Singleton#Python3#class MyClass (Metaclass=singleton):#Pass
__CALL__ calls itself like a function.
A singleton pattern in Python