Introduction
Registration tree mode is also called registration mode or registrar mode. The registration tree pattern is a pattern design method that registers object instances to a global object tree and picks from the object tree when needed. Unlike fruit trees, fruits can only be picked once, but instances on the registered tree can be picked countless times.
After using the registration tree mode, we can better coordinate and manage instances, which is as convenient and practical as using global variables.
Implementation
I will demonstrate it to you in the form of code below. Please refer to the code comments for details
Code:
<?php header('Content-Type:text/html;charset=utf-8'); /** * Class Register 注册树类 */ class Register { // 树的枝干-用于储存树上的果实(实例) public static $objects; /** * 将实例插入注册树中 * * @param $alias 对象别名-注册树中的名称 * @param $object 对象实例 */ public static function set($alias, $object) { self::$objects[$alias] = $object; } /** * 从注册树中读取实例 * * @param $alias 对象别名-注册树中的名称 * * @return mixed 返回的对象实例 */ public static function get($alias) { if (isset(self::$objects[$alias])) { return self::$objects[$alias]; } else { echo '您要找的对象实例不存在哦<br>'; } } /** * 销毁注册树中的实例 * * @param $alias 对象别名-注册树中的名称 */ public static function _unset($alias) { unset(self::$objects[$alias]); } } /** * Class demo 演示类 */ class demo { /* * 测试方法 */ public function test() { echo '看这里看这里<br><br>'; } } // 实例化测试类,获取对象实例 $demo = new demo(); // 注册到树上 $tree = Register::set('de', $demo); // 取出来 $de_true = Register::get('de'); // 测试 $de_true->test(); // 销毁 Register::_unset('de'); // 尝试再次取出来 $de_true_two = Register::get('de'); // 尝试再次测试 $de_true_two->test();
Running results:
// 第一次成功运行 看这里看这里 // 第二次销毁后找不到对象实例 您要找的对象实例不存在哦 // 第二次销毁后找不到对象实例,故无法调用对象方法,报错 Fatal error: Call to a member function test() on a non-object in E:\phpStudy\WWW\test\test.php on line 77
Extension
The above has shown you how to build the registration tree pattern through examples , I believe everyone should understand it, so below we will combine the registration tree mode, factory mode and singleton mode to see how to implement their code
Code:
<?php header('Content-Type:text/html;charset=utf-8'); /** * Class Register 注册树类 */ class Register { // 树的枝干-用于储存树上的果实(实例) public static $objects; /** * 将实例插入注册树中 * * @param $alias 对象别名-注册树中的名称 * @param $object 对象实例 */ public static function set($alias, $object) { self::$objects[$alias] = $object; } /** * 从注册树中读取实例 * * @param $alias 对象别名-注册树中的名称 * * @return mixed 返回的对象实例 */ public static function get($alias) { if (isset(self::$objects[$alias])) { return self::$objects[$alias]; } else { echo '您要找的对象实例不存在哦<br>'; } } /** * 销毁注册树中的实例 * * @param $alias 对象别名-注册树中的名称 */ public static function _unset($alias) { unset(self::$objects[$alias]); } } /** * Class demo 单例演示类 */ class singleDemo { // 存放实例属性 public static $_instance; public $content; // 私有化构造函数,防止外部new private function __construct($content) { $this->content = $content; } // 获取实例 public static function getInstance($content) { // 判断实例是否存在 if (!(self::$_instance instanceof self)) { self::$_instance = new self($content); } return self::$_instance; } // 私有化克隆方法,防止克隆 private function __clone() { } // 测试方法 public function test() { echo $this->content.'<br>'; } } /** * Class factory 工厂类 */ class Factory { // 生产 public static function create($content) { // 返回对象 return singleDemo::getInstance($content); } } // 生产并返回对象实例 $object = Factory::create('我是content参数内容,哈哈'); // 执行测试方法 $object->test();
Running results:
我是content参数内容,哈哈