Common scenarios for single-case mode (Singleton)

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/likika2012/article/details/11483167

The singleton mode (Singleton) is also called the single-state mode, is the simplest mode in the design pattern, even some model masters do not call it a pattern, called it as a realization technique, because the design pattern emphasizes the abstraction of the relationship between objects, and the singleton pattern only has its own object, So there are some design masters and call it one of the design patterns.

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

A lot of people who have not been used, may think, the singleton mode does not feel how to use, the actual application scenarios? Below, I'll list some of the more interesting single-case scenarios that are around us.

1. Task Manager for Windows is a very typical singleton mode (this is familiar), think about it, can you open two Windows Task Manager? Don't believe you try it yourself oh ~

2. Windows Recycle Bin (Recycle Bin) is also a typical single-instance application. During the whole system operation , the Recycle Bin maintains the only one instance.

3. The website counter, generally also uses the Singleton pattern realization, otherwise difficult to synchronize.

4. Application of the log application, the general use of single-case mode implementation, which is generally due to the shared log file is open, because there can only be one instance to operate, otherwise the content is not good to append.

5. The Web application's configuration object reads, generally also applies the singleton pattern, this is because the configuration file is the shared resource.

6. database connection pooling is generally designed with a singleton pattern, because database connections are a database resource. Database software system in the use of database connection pool, mainly to save the open or close the database connection caused by the efficiency of loss, this loss of efficiency is very expensive, because how to use a single case mode to maintain, can greatly reduce this loss.

7. Multithreaded thread pools are generally designed with a singleton pattern, because thread pooling is convenient for controlling the threads in the pool.

8. The file system of the operating system is also a specific example of a large single-instance pattern implementation, and an operating system can have only one file system.

9. HttpApplication is also a typical application of unit cases. People familiar with the entire request lifecycle of ASP. NET (IIS) should know that HttpApplication is also a singleton mode, and all HttpModule share a HttpApplication instance.

Summarized above, it is not difficult to see:

Scenarios where a singleton pattern is applied are 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 resource control, convenient communication between resources. such as the thread pool.

Common application scenarios for (GO) singleton mode (Singleton)

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.