PHP design pattern of single case, multi-example design mode application Analysis _php tutorial

Source: Internet
Author: User
The singleton (Singleton) mode and the Uncommon Multiple cases (Multiton) mode control the number of classes in the application. such as the schema name, Singleton can only be instantiated once, only one object, multiple instances of the pattern 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. However, it is sometimes not recommended to use singleton because it generates a global state and

The single Root object does not encapsulate any system functionality. In most cases, unit testing and debugging can become difficult. The reader decides according to the circumstances.
code Example:
Copy CodeThe 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 the variable without having to make it a static variable of the class
if ($instance ==null) {
$instance =new singletonexample ();
}
return $instance;
}
}
$obj 1=singletonexample::getinstance ();
$obj 2=singletonexample::getinstance ();
Var_dump ($obj 1=== $obj 2);//True is the same instance
?>

Multiton is similar to singleton, but the latter requires the getinstance () function to pass the key values.
For a given key value there will only be a unique object instance, if there are multiple nodes, each node has a unique identifier, and each node in a single execution (such as the node in the CMS) may occur multiple times, then you can use Multiton mode to implement these nodes Ah, Multiton save memory, and ensure that multiple instances of the same object do not conflict.
Example:
Copy CodeThe 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);
}
};
?>

http://www.bkjia.com/PHPjc/327983.html www.bkjia.com true http://www.bkjia.com/PHPjc/327983.html techarticle The Singleton (Singleton) mode and the Uncommon Multiple cases (Multiton) mode control the number of classes in the application. such as the pattern name, Singleton can only be instantiated once, only one object, multi-sample mode can ...

  • 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.