Singleton pattern (Singleton) is also called the Singleton pattern, is one of the most simple design pattern mode, and even a few pattern master, don’t call it the model, referred to as a technique to implement, because pay attention to the relationship between the object of abstract design mode, and Singleton pattern only own a object, and therefore some design masters and treat it as one of design patterns.

I’m not going to go into the details of how to implement the singleton pattern or how it works (because this is conveniently covered in too many good articles).

A lot of people who haven’t used it very much might think that singletons don’t feel very useful, but what are the actual application scenarios? Below, I’ll list some of the singleton application scenarios that are around us and make sense.

  1. Windows Task Manager is a typical singleton. Can you open two Windows Task Managers? Do not believe you have a try oh ~

  2. The Windows Recycle Bin is also a typical singleton application. The recycle bin maintains only one instance throughout the entire system operation.

  3. The counter of the website is generally realized by singleton mode, otherwise it is difficult to synchronize.

  4. Application logging is usually not implemented in singleton mode because the shared log file is always open, because only one instance can operate, otherwise the content cannot be appended.

  5. The singleton mode is also used to read configuration objects in Web applications, because configuration files are shared resources.

  6. Database connection pooling is also typically designed in a singleton pattern because a database connection is a database resource. The use of database connection pool in database software system is mainly to save the efficiency loss caused by opening or closing database connection. This efficiency loss is very expensive, because it can be greatly reduced by using singleton mode to maintain.

  7. Multithreaded thread pools are also designed in a singleton mode because thread pools are designed to facilitate the control of threads in the pool.

  8. The file system of an operating system is also a concrete example of a large singleton pattern implementation. An operating system can have only one file system.

  9. HttpApplication is also a typical use of unit cases. Those familiar with the entire request lifecycle of ASP.Net(IIS) should know that HttpApplication is also a singleton pattern, with all HttpModules sharing an Instance of HttpApplication.

To sum up the above, it is not difficult to see:

The application scenarios of singleton mode are generally found under the following conditions:

(1) In the case of resource sharing, avoid performance or loss caused by resource operation. As in the log files above, apply the configuration.

(2) In the case of controlling resources, it facilitates the communication between resources. Such as thread pools.

The above content hopes to help you, more free PHP factory PDF, PHP advanced architecture video materials, PHP wonderful good article can be wechat search concerns: PHP open source community

2021 Jinsanyin four big factory interview real questions collection, must see!

Four years of PHP technical articles collation collection – PHP framework

A collection of four years’ worth of PHP technical articles – Microservices Architecture

Distributed Architecture is a four-year collection of PHP technical articles

Four years of PHP technical essays – High Concurrency scenarios

Four years of elite PHP technical article collation collection – database