Home >Backend Development >Python Tutorial >What is the use of singleton mode python
Singleton mode is also called singleton mode. It is the simplest mode among design patterns. Even some pattern masters do not call it a pattern, but call it an implementation technique because design patterns pay attention to objects. The abstraction of the relationship between objects, while the singleton pattern has only one object of its own, so some design masters call it one of the design patterns.
Logical model diagram:
Many people who have never used it may think that the singleton mode is not used very much. What are the actual application scenarios? Below, I will list some singleton application scenarios that are around us and are very meaningful. Related recommendations: "
Python Video Tutorial"
1. Windows' Task Manager (Task Manager) is a very typical singleton mode. Think about it, it is No, can you open two windows task managers? 2. The Recycle Bin of Windows is also a typical singleton application. During the entire system operation, the Recycle Bin maintains only one instance. 3. Website counters are generally implemented in singleton mode, otherwise it will be difficult to synchronize. 4. Application log applications are generally implemented in singleton mode. This is usually because the shared log file is always open because there can only be one instance to operate it, otherwise it is difficult to append the content. . 5. When reading the configuration object of a web application, the singleton mode is generally used. This is because the configuration file is a shared resource. 6. The design of database connection pool generally adopts singleton mode, because database connection is a database resource. The main purpose of using database connection pools in database software systems is to save the efficiency loss caused by opening or closing database connections. This efficiency loss is still very expensive, because using singleton mode for maintenance can greatly reduce this loss. . 7. The design of multi-threaded thread pools generally adopts singleton mode. This is because the thread pool needs to facilitate the control of the threads in the pool. 8. The file system of the operating system is also a specific example of the implementation of the large singleton mode. An operating system can only have one file system. 9. HttpApplication is also a typical application of unit cases. People who are familiar with the entire request life cycle of ASP.Net (IIS) should know that HttpApplication is also a singleton mode. All HttpModules share an HttpApplication instance. To summarize the above, it is not difficult to see: Singleton mode application scenarios are generally found under the following conditions: (1) In the case of resource sharing, avoid performance or losses caused by resource operations. As in the log file above, apply the configuration. (2) When controlling resources, it facilitates mutual communication between resources. Such as thread pool, etc.The above is the detailed content of What is the use of singleton mode python. For more information, please follow other related articles on the PHP Chinese website!