Analysis of Registry Tree Pattern of PHP Design Pattern

  • 2021-09-11 19:36:23
  • OfStack

This article illustrates the PHP registry tree pattern. Share it for your reference, as follows:

What is the registry tree pattern?

Of course, the registry tree mode is also called registry mode and registrar mode. The reason why I am here melodramatic 1 under its name, because I feel that the name of the registry tree is easier for people to understand. Like the first two articles 1, our article still starts with the name. Registry tree pattern is designed by registering an object instance to a global object tree and picking it from the object tree when necessary. This reminds me of buying candied haws when I was a child. The candied haws sellers put candied haws on a big pole, and people took them down when they bought them. The difference is that there will be registration tree mode, which can be picked many times, but candied haws will be lost once. . .

Why do you want to use the registry tree mode?

Singleton pattern solves the problem of how to create only one object instance in the whole project, while factory pattern solves the problem of how to create instance object without new. So what problem does the registry tree pattern want to solve? Before considering this issue, it is necessary for us to consider the limitations faced by the first two models at present. First of all, the process of creating only one object in singleton pattern itself has one kind of judgment, that is, judging whether the object exists or not. The object is returned if it exists, and the object is created and returned if it does not exist. Every time an instance object is created, there must be such a level 1 judgment. The factory model is more concerned with extended maintenance. Generally speaking, singleton pattern and factory pattern can produce more reasonable objects. How is it convenient to call these objects? Moreover, the objects established in this way in the project seem to be stragglers, so it is inconvenient to make overall management arrangements. Therefore, the registry tree pattern came into being. Whether you generate objects from singleton mode, factory mode or a combination of the two, all of them are "plugged" into the registration tree for me. When I use an object, just take 1 from the registration tree. This is as convenient and practical as using global variable 1. And the registry tree pattern also provides a very good idea for other patterns.

How do I implement a registry tree?

Through the above description, we seem to find a solution easily. First of all, we need a class as a registry tree, which is beyond doubt. All objects are "inserted" into the registration tree. This registry tree should be served by a static variable. And the registry tree should be a 2-dimensional array. This class should have 1 method to insert an object instance ( set() ), when the corresponding method should have 1 undo object instance ( _unset() ). Of course, the most important thing is to have a method to read the object ( get() ). With these, we can happily complete the registry tree mode ~ ~ ~

Let's make a small combination of the three modes. Simply creating an instance object is far from complicated, but when used in large projects, the convenience is self-evident.


<?php
// Create a singleton 
class Single{
  public $hash;
  static protected $ins=null;
  final protected function __construct(){
    $this->hash=rand(1,9999);
  }
  static public function getInstance(){
    if (self::$ins instanceof self) {
      return self::$ins;
    }
    self::$ins=new self();
    return self::$ins;
  }
}
// Factory model 
class RandFactory{
  public static function factory(){
    return Single::getInstance();
  }
}
// Registry tree 
class Register{
  protected static $objects;
  public static function set($alias,$object){
    self::$objects[$alias]=$object;
  }
  public static function get($alias){
    return self::$objects[$alias];
  }
  public static function _unset($alias){
    unset(self::$objects[$alias]);
  }
}
Register::set('rand',RandFactory::factory());
$object=Register::get('rand');
print_r($object);

At this point, the design of three modes is introduced. Each pattern design itself will complement each other, and when introducing other patterns later, one or more other design patterns will be used more or less.

It doesn't matter if you don't understand a mode. I believe that with the deepening of programming, you will have a sense of surprise. I hope you can make progress with me.

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: