SoFunction
Updated on 2024-10-29

Python singleton pattern of the four ways to create an example of analysis

singleton model

The Singleton Pattern is a common software design pattern whose main purpose is to ensure that only one instance of a class exists. A singleton object comes in handy when you want to have only one instance of a class in the entire system.

For example, the configuration information of a server program is stored in a file, and the client reads the configuration file through an AppConfig class. If the contents of the configuration file are used in many places during the running of the program, i.e., instances of the AppConfig object need to be created in many places, this leads to the existence of multiple instances of AppConfig in the system, which can be a serious waste of memory resources, especially in the case of a large number of configuration file contents. In fact, for a class like AppConfig, we would like to have only one instance of the object during the runtime of the program.

In Python, we can implement the singleton pattern in a number of ways:

  • Using Modules
  • Use __new__ to make
  • With decorator
  • Use of metaclasses

Using Modules

In fact, Python's module is the natural singleton pattern, because the module will generate the .pyc file when it is imported for the first time, and when it is imported for the second time, it will load the .pyc file directly without executing the module code again. Therefore, we only need to define the relevant functions and data in a module to get a singleton object. If we really want a singleton class, consider this:

Save the above code in a file and use it like this:

Using __new__

In order to make only one instance of the class, we can use __new__ to control the instance creation process with the following code:

In the above code, we associate an instance of the class with a class variable _instance, which creates the instance if cls._instance is None, otherwise it returns cls._instance directly.

The status of implementation is as follows:

Using Decorators

We know that a decorator can dynamically modify the functionality of a class or function. Here, we can also use a decorator to decorate a class so that it can only generate one instance, the code is as follows:

In the above, we defined a decorator singleton, which returns an internal function getinstance that determines whether a class is in the dictionary instances, and if it is not, it stores cls as key and cls(*args, **kw) as value in instances, otherwise, it directly return instances[cls].

Using metaclass

A metaclass controls the class creation process, and it does three main things:

Intercepting class creation modifies the class definition to return the modified class

The code to implement the singleton pattern using metaclasses is as follows:

Summary Python's modules are naturally singleton, which should be sufficient in most cases, but of course, we can also use decorators, metaclasses, etc.

This is the whole content of this article.