A common application scenario for PHP single example mode

Source: Internet
Author: User
Tags connection pooling


The single case mode (Singleton) is also called the single State pattern, is the simplest pattern in the design pattern, even some model masters do not call it the pattern, called it a realization skill, because the design pattern pays attention to the abstract of the relation between objects, and the single case pattern only has one object, Some design gurus have also called it one of the design patterns.

Here is not specifically how to implement a single example mode and introduce its principle (because this is convenient already have too many good articles introduced), if the single case mode does not understand can first look at: http://terrylee.cnblogs.com/archive/2005/12/09/293509.html. Of course, you can search yourself.

A lot of people who haven't used it are likely to think that the single case pattern doesn't feel very good, what is the actual application scenario? Below, I'll list some of the very interesting case scenarios that are around us.

1. Windows Task Manager (Task Manager) is a typical single case pattern (this is familiar), and think about it, can you open two Windows Task Manager? Do not believe you try Oh ~

2. Windows Recycle Bin (Recycle Bin) is also a typical single case application. The Recycle Bin maintains only one instance of the system during its operation.

3. The counter of the website, generally also adopts the single example pattern realization, otherwise difficult synchronization.

4. Application log application, generally how to use a single example mode, this is generally because the shared log file is always open, because only one instance to operate, otherwise the content is not good to append.

5. Read the Configuration object of the Web application and generally apply the single case mode, which is because the configuration file is a shared resource.

6. Database connection pooling is generally designed to take the form of a single case, because a database connection is a database resource. Database software system in the use of database connection pool, mainly to save on or off the database connection caused by the efficiency of the loss, this efficiency is very expensive, because of the single case mode to maintain, you can greatly reduce this loss.

7. Multithreaded thread pool design is generally a single case mode, this is because the thread pool to facilitate the control of the thread in the pool.

8. The operating system file system, but also a large single example of the implementation of a specific example, an operating system can only have one file system.

9. HttpApplication is also a typical application of the unit example. People familiar with the entire request lifecycle of asp.net (IIS) should know that HttpApplication is also a singleton pattern, and that all HttpModule share a HttpApplication instance.

Summing up the above, it is not difficult to see:

A single case pattern application scenario is generally found under the following conditions:

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

(2) In the case of controlling resources, it is convenient for communication between resources. such as thread pool.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.