php design mode-registry tree mode

php registry tree mode

1. What is the registration tree mode?

Registered Tree mode registers object instances on the global object tree, removing objects from the global object tree when needed, just like buying candied gourds when you were a kid. Candied gourds are put on a big stick and people take them off when they buy them. The difference is, the registered tree mode also takes them off., can pick many times, candied gourd picks once, hey hey.

2. Why use factory mode?

The singleton pattern described earlier solves the problem of creating unique objects throughout the project.

Factory mode solves the problem of not solving instance objects with new, and considers project expansion and maintenance.

Overall, the singleton and factory models can produce more reasonable objects.

So how can I easily call these objects as a whole? At this time, the registered tree mode is used. Regardless of whether you are a singleton mode, a factory mode or an object generated by the combination of the two, all of them are registered to the tree for me. When using an object, it is better to take it directly from the tree.

3. How to implement the registration tree

We use "Four One" to remember

  • A static variable that "inserts" all objects into the registry tree, which should be a two-dimensional array.
  • A method to insert an instance of an object (set()),
  • A method to undo an instance of an object (_unset()).
  • A method to read objects (get()).

With these, we can happily complete the registration tree mode~~

4. Code examples

/**
 * @purpose: Create a singleton class
 * Class Single
 */
class Single {
    /**
     * @var Object Save static member variables for class instances
     */
    private static $_instance;

    /**
     * Single constructor. Private construction methods
     */
    private function __construct(){
        echo 'This is a Constructed method;';
    }

    /**
     * @purpose: Create u clone method to prevent object from being copied and cloned
     */
    public function __clone(){
        trigger_error('Clone is not allow!',E_USER_ERROR);
    }

    /**
     * @return Single|Object Singleton method, a common static method for accessing instances
     */
    public static function getInstance(){
        if(!(self::$_instance instanceof self)){
            self::$_instance = new self;
        }
        return self::$_instance;
    }

}

/**
 * @purpose: Create a factory class
 * Class mysqlFactory
 */
class MysqlFactory{

    static public function factory($className){
        return new $className();
    }

}

interface  Mysql{

    public function connect();
}

class Mysqli  implements mysql{

    public  function connect(){
        echo 'mysqli';
    }
}

class Pdo implements mysql{

    public function connect(){
        echo 'pdo';
    }
}

/**
 * @purpose: Create a registration tree pattern
 * Class Register
 */
class Register
{
    /**
     * @var array Insert all objects into the registration tree
     */
    static protected $objects;

    /**
     * Insert Object Instance
     * @param string $alias
     * @param object $object Object Instance
     */
    public static function set($alias,$object)
    {
        self::$objects[$alias]=$object;
    }

    /**
     * Undo Object Instance
     * @param string $alias
     */
    public static function _unset($alias)
    {
        unset(self::$objects[$alias]);
    }

    /**
     * Get Object Instances
     * @param string $alias
     * return object
     */
    public static function get($alias)
    {
        return self::$objects[$alias];
    }
}

//Register database objects generated by factories and units on the Registry Tree
Register::set('signal',MysqlFactory::factory('Pdo'));
$signal=Register::get('signal');
$signal->connect();

This article is written after referring to the blog, thank you for your blog: https://www.cnblogs.com/freelyflying/p/7019243.html

If reproduced, please indicate the source: https://www.cnblogs.com/chrdai/p/11182796.html

Keywords: PHP PDO MySQL Database

Added by phpr0ck5 on Sat, 13 Jul 2019 19:44:38 +0300