PHP Core Learning-Design pattern Learning-Registration Tree mode

Source: Internet
Author: User
Written in the front: Design pattern learning needs to be brought into the scene to learn, then summed up, will find the old driver routines of the United States, this article with such a course factory model of a problem, if our customers need to buy a book, by the way, the wine also inquires out, the general writing is that we first create a book instance, Then call the query, in the call to the wine instance, and then call the Query method, the amount of data is small, it feels good, but, when we have a lot of words, become very bloated, then now we have to learn a new design pattern-registration tree mode to solve this problem.
What is the registration tree mode.


Registration tree mode is also called registration mode, the Registrar mode. The registration tree mode is the pattern design method that is picked from the object tree when it is required by registering the object instance on a global object tree.


Why the registration tree mode is used.


The singleton pattern solves the problem of how to create a unique object instance throughout the project, and the factory pattern solves the method of how to create an instance object without new. So what does the registration tree model want to solve?  Before we consider this issue, we still need to consider the limitations of the first two models that are currently under way. First, the process of creating a unique object in a single pattern also has a judgment that determines whether an object exists. exists, the object is returned, and the object is created and returned. Every time you create an instance object, there is a level of judgment. The factory model is more concerned with the issue of extended maintenance. In general, a single case model and a factory model can produce more reasonable objects. How to make it easier to call these objects. And in the project so set up objects like stragglers, it is inconvenient to co-ordinate management arrangements AH. Therefore, the registration tree model arises. Whether you're using a single model, a factory model, or a combination of the two objects, all of them are "plugged into" the registration tree. When I use an object, just take it from the registration tree. This is as convenient and practical as we use global variables. And the registration tree model provides a very good idea for other schemas.


How to implement the registration tree.


By the above description, we seem to have found a solution easily. First of all we need a class as a registration tree, which is beyond doubt. All objects are "inserted" into the registration tree. This registration tree should be a static variable to act as. And this registration tree should be an array. This class should have a method for inserting an instance of an object (set ()), which should have a method to undo the object instance (_unset ()) when it is corresponding. The most important thing, of course, is to have a method of reading the object (get ()) and to read the object's method we use the Magic method to traverse out the properties of the instantiated class. With these, we can happily complete the registration tree mode ~ ~ ~


The previous product class here is not repeated, focusing on the implementation of the registration tree, implemented as follows:
<?php
 class  productdatacenter
 {
     public  static $objectList =[];//object array, save
// Inserts the instantiated object into a static array variable public
     static function set ($k, $v)
     {
         self:: $objectList [$k]= $v;
     }
Delete array public
     static function remove ($k)
     {
         unset (self:: $objectList [$k]);
     }
    /* public static function Get ($k)
     {return
         self:: $objectList [$k];
     } ///////////////
         ////////////////Todo:implement ($name, $arguments) __callstatic __callstatic () method.
         $return =[];//Default return value
        foreach (self:: $objectList as $k => $v)
{
if (method_exists ($v, $name))
{
$ret = $v-> $name ($arguments);
if ($ret)
{
$return []= $ret;
}
}} return $return;
}




How the factory class calls the registration tree:
<?php
require ("iproduct.php");
Require ("productdatacenter.php");
Class Productfactory
{
    //Commodity factory class
    static  function getproduct ($type)
    {
        $obj =false;
        if (!class_exists ($type))
        {
            require ($type.). PHP ");
        }
        Switch ($type)
        {case
            ' books ':
                $obj =new Books ();
                break;
            Case "Dogs":
                $obj =new Dogs ();
                break;
            Case "Wines":
                $obj =new Wines ();
                break;
        if (is_subclass_of ($obj, "iproduct"))
            Productdatacenter::set ($type, $obj);//Put the created object into the object Tree


    }
}


Customer Calls
Productfactory::getproduct (["Books", "Dogs"]);
Var_export (Productdatacenter::getlist ()


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.