Application Analysis of single-case and multi-case design patterns in php design patterns

Source: Internet
Author: User
This article gives a detailed analysis of the single-case and multi-case application in the php design mode. For more information, see Singleton) the pattern and the uncommon Multiton pattern 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:
The code is as follows:
Class SingletonExample {
Private function _ construct () {}// prevents direct instantiation
Public static function getInstance () {// not associated with any object
Static $ instance = null; // all code that calls this function shares this variable, so it is not necessary to make it a static variable of the class.
If ($ instance = null ){
$ Instance = new SingletonExample ();
}
Return $ instance;
}
}
$ Obj1 = SingletonExample: getInstance ();
$ Obj2 = SingletonExample: getInstance ();
Var_dump ($ obj1 ===$ obj2); // true is the same instance.
?>

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:
The code is as follows:
Class MultitonExample {
Private function _ construct () {}// prevents direct instantiation

Public static function getInstance ($ key ){
Static $ instance = array ();
If (! Array_key_exists ($ key, $ instance )){
$ Instance [$ key] = new SingletonExample ();
}
Return $ instance ($ key );
}
};
?>

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.