PHP design mode (c), PHP design mode
This model is relatively simple and good understanding, in the PHP framework is often used, in some larger PHP framework, in the initialization of some of the common class instances in the Registrar, there is a static array in the Registrar class, in the future, if you want to use it, directly according to the name can be obtained to the register to save the instance. Without having to go to the new class, getinstance, or factory method again. I feel that this pattern is a bit like combining a singleton pattern with a simple factory pattern, which is then saved in a static array (like a static variable in a singleton mode), and can then be used to obtain registered instances based on different parameters, which, compared to the simple factory model, can be added dynamically, Instead of having to modify the code, it is important to note that you must register before you can get it.
There are several points to note about this model:
1. The first is to have a class to manage the instance to be registered (for example: register), we will add him to register the class bar.
2. This registration class must have a static array variable, using a key-value array (hash) to store these registered instances, where the key corresponds to the instance name, and the value corresponds to the instance.
3. Of course, _set, _get, _unset (with the underscore start simply because the unset is the keyword, and then for the sake of unification, all added) These methods are not less, used to set up, get, and destroy the contents of the Registrar.
4. In fact, this registration class can be regarded as a loading and unloading process, before the use of loading, when the instance is not in use can be uninstalled.
4. It is also important to note that it is not possible to put too many instances in, and some instances can be destroyed by one time without saving to the Registrar, so as not to waste space.
5. A different name in the registrar can be saved with the same instance because he has a name as the index when it is saved.
Php/** * Registrar mode-Example * @author Yan (luluyrt@163.com)*/classregister{/** * @var array $objects * variables to hold registered instances*/ Private Static $_objects=Array(); /** * Register an instance into the Registrar * @param string $name The class name to register * @param object $obj class instance to register * @return true*/ Public Static function_set ($name,$obj){ if(!isset(Self::$_objects[$name]) { self::$_objects[$name] =$obj; } return true; } /** * Remove an instance from the Registrar * @param string $name The class name to register * @return true*/ Public Static function_unset ($name){ if(isset(Self::$_objects[$name])){ unset(Self::$_objects[$name]); } return true; } /** * Get an instance from the Registrar * @param string $name The class name to register * @return Object/false*/ Public Static function_get ($name){ if(isset(Self::$_objects[$name])){ returnSelf::$_objects[$name]; } return false; } }classman{ Public functionsay () {Echo"My name is users!!"; }}register:: _set (' Man1 ',NewMan ()); Register:: _get (' Man1 ')->say ();
Send me~
http://www.bkjia.com/PHPjc/920882.html www.bkjia.com true http://www.bkjia.com/PHPjc/920882.html techarticle PHP design mode (c), PHP design mode Registrar mode This mode is relatively simple to understand, in the PHP framework is often used, in some of the larger PHP framework, in the initialization of a ...