Application analysis _php techniques of single case and multiple design patterns of PHP design pattern

Source: Internet
Author: User
The single example (Singleton) pattern and the Uncommon Multiple (Multiton) pattern control the number of classes in the application. such as the pattern name, a singleton can only be instantiated once, with only one object, and multiple instances can be instantiated multiple times.

Based on Singleton features, 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 situation.
code Example:
Copy Code code as follows:

<?php
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, which requires the getinstance () function to pass key values.
Only a unique object instance exists for a given key value. If there are multiple nodes, each node has a unique table identifier, and each node in a single execution (such as the node in the CMS) may occur several times, then you can use the Multiton mode to implement these nodes Ah, Multiton save memory, and ensure that multiple instances of the same object do not collide.
Example:
Copy Code code as follows:

<?php
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.