Php design pattern-single case and multi-case design pattern-PHP Tutorial

Source: Internet
Author: User
Php design mode: single-case and multi-case design mode. Singleton mode and the uncommon Multiton mode control the number of classes in an application. For example, the mode name can only be instantiated once, with only one object. The Singleton mode and the uncommon Multiton mode control the number of classes in the application. For example, the mode name can only be instantiated once for a single instance and only one object. the mode can be instantiated multiple times.

Based on the Singleton feature, we often use Singleton to configure the application and define variables that may be accessed at any time in the application. But sometimes Singleton is not recommended because it generates a global state and

This single root object does not encapsulate any system functions. In most cases, unit testing and debugging are difficult. The reader decides based on the actual situation.

Sample code:

 

Multiton is similar to singleton. The difference is that the latter requires the getInstance () function to pass key values.

For a given key value, only a unique object instance exists. if multiple nodes exist, each node has a unique table identifier, in addition, each node may appear multiple times in a single execution (such as a node in cms), so you can use the Multiton mode to implement these nodes. the Multiton mode saves memory, make sure that multiple instances of the same object do not conflict.

Example:

 
 

References: php and mysql expert programming

 

The Singleton mode and the uncommon Multiton mode control the number of classes in the application. For example, the mode name can only be instantiated once. there is only one object. the mode of multiple instances can be...

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.