Analysis of singleton and multiple php design patterns
- 2020-07-21 07:04:43
- OfStack
The singleton (Singleton) pattern and the unusual multi-instance (Multiton) pattern control the number of classes in an application. For example, schema names, singletons can only be instantiated once, only one object, and multi-instance schemas can be instantiated multiple times.
Based on the features of Singleton, we often use Singleton to configure applications and define variables that can be accessed from time to time in the application. However, Singleton is sometimes not recommended because it generates a global state and
The single-root object does not encapsulate any system functionality. In most cases, this makes unit testing and debugging difficult. The reader decides as the case may be.
Code examples:
Multiton is similar to singleton except that the latter requires the getInstance() function to pass key values.
For a given key value will only exist only 1 object instance, if there are multiple nodes, each node has only 1 table general character, and each node in a single execution (such as in cms node) may appear many times, you can use Multiton pattern implementation these nodes, Multiton save memory, and to ensure that an object with multiple instances of the conflict.
Example:
Based on the features of Singleton, we often use Singleton to configure applications and define variables that can be accessed from time to time in the application. However, Singleton is sometimes not recommended because it generates a global state and
The single-root object does not encapsulate any system functionality. In most cases, this makes unit testing and debugging difficult. The reader decides as the case may be.
Code examples:
<?php
class SingletonExample{
private function __construct(){}// Prevent direct instantiation
public static function getInstance(){ // It is not associated with any object
static $instance=null; // All the code that calls this function shares this variable and does not need to be 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 1 An instance
?>
Multiton is similar to singleton except that the latter requires the getInstance() function to pass key values.
For a given key value will only exist only 1 object instance, if there are multiple nodes, each node has only 1 table general character, and each node in a single execution (such as in cms node) may appear many times, you can use Multiton pattern implementation these nodes, Multiton save memory, and to ensure that an object with multiple instances of the conflict.
Example:
<?php
class MultitonExample{
private function __construct(){}// Prevent direct instantiation
public static function getInstance($key){
static $instance=array();
if(!array_key_exists($key,$instance)){
$instance[$key]=new SingletonExample();
}
return $instance($key);
}
};
?>