Home >php教程 >PHP开发 >PHP design pattern registration tree pattern

PHP design pattern registration tree pattern

高洛峰
高洛峰Original
2016-11-16 10:04:051266browse

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(&#39;Content-Type:text/html;charset=utf-8&#39;);
/**
 * 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 &#39;您要找的对象实例不存在哦<br>&#39;;
        }

    }

    /**
     * 销毁注册树中的实例
     *
     * @param $alias 对象别名-注册树中的名称
     */
    public static function _unset($alias)
    {
        unset(self::$objects[$alias]);
    }
}

/**
 * Class demo 演示类
 */
class demo
{
    /*
     * 测试方法
     */
    public function test()
    {
        echo &#39;看这里看这里<br><br>&#39;;
    }
}

// 实例化测试类,获取对象实例
$demo = new demo();
// 注册到树上
$tree = Register::set(&#39;de&#39;, $demo);
// 取出来
$de_true = Register::get(&#39;de&#39;);
// 测试
$de_true->test();
// 销毁
Register::_unset(&#39;de&#39;);
// 尝试再次取出来
$de_true_two = Register::get(&#39;de&#39;);
// 尝试再次测试
$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(&#39;Content-Type:text/html;charset=utf-8&#39;);
/**
 * 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 &#39;您要找的对象实例不存在哦<br>&#39;;
        }

    }

    /**
     * 销毁注册树中的实例
     *
     * @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.&#39;<br>&#39;;
    }

}

/**
 * Class factory 工厂类
 */
class Factory
{
    // 生产
    public static function create($content)
    {
        // 返回对象
        return singleDemo::getInstance($content);
    }
}

// 生产并返回对象实例
$object = Factory::create(&#39;我是content参数内容,哈哈&#39;);
// 执行测试方法
$object->test();

Running results:

我是content参数内容,哈哈


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn