A singleton pattern of design patterns

Source: Internet
Author: User

Wikipedia on the definition of a singleton mode: Also known as the list mode, is a common software design pattern. When you apply this pattern, the class of the Singleton object must guarantee that only one instance exists. Many times the entire system needs to have only one global object, which helps us coordinate the overall behavior of the system.

For a slightly more complex system, the various data to be managed (such as database connection, module configuration information, etc.), it is best to encapsulate this information, all the module to use this information can only be taken from here, this can reduce the coupling between each module, Facilitates the maintenance of late procedures.

Here are a few examples of common scenarios for a singleton pattern:
Manage database connections: If you create a database connection directly in each model, you might create dozens of connections on a single visit, which is a waste of database resources, and it is sufficient for database connections to create only one database connection per request.

Manage Profiles: Configuration information is often used in. xml files or directly using arrays to encapsulate, for the component system, the configuration file is the core of the entire system, if each module is directly read the configuration file, not only very cumbersome, and not conducive to management, if the configuration file structure has changed, you need to each module in each Once the configuration information is called for modification, this is very painful. With the singleton mode, it is convenient to manage configuration information uniformly, if the configuration file structure changes, only need to modify the invocation method in the singleton, which is very advantageous to the system's late maintenance.

Log output: In the module development and debugging, operation, often need to use the log function, if in a single case, the static log method, you can easily call directly and avoid in the code everywhere Echo, Var_dump, Print_r and so on. The method in a singleton can be called globally only once, so it's worth taking the time to write it better and more fully functional.

For the MVC architecture system, the data exchange between the controller layer and the view layer can be done through a singleton mode. The data in the controller is then "forwarded" to the view layer by a single case.

The singleton can also be used to temporarily "store" the model instance in order to facilitate the use of the model instance in subsequent code. Instantiated model, what should I do if I want to continue to use it in other classes? The direct new model object is obviously not possible, the answer is to use a singleton.

The above simply lists some of the scenarios I used in the development of the singleton mode, there are many applications are not listed, in short, the singleton mode is a very useful design pattern, as a program developer needs to be fully mastered.

This article copyright belongs to the author, reproduced please indicate the source: http://www.cnblogs.com/jpdoutop/p/Design-Pattern-Singleton.html

A singleton pattern of design patterns

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.