The registered tree mode of PHP design mode

Source: Internet
Author: User

What is a registered tree mode? "Global share and Swap objects"

 Registration tree mode is also called registration mode, Registrar mode.   The registration tree mode is designed by registering the object instance on a global object tree, and then picking the pattern from the object tree when needed. This reminds me of the childhood buy candied fruit, sell candied fruit will candied fruit inserted in a big pole, people buy when take down. The difference is, the registration tree mode pick down will have, can pick a lot of times, candied fruit pick once no ...

Why should I use the registration tree mode?

  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 not building the instance object through new. So what's the problem with registering tree mode?  Before considering this issue, it is necessary to consider the limitations that the first two models are currently facing. First, the process of creating a unique object in a singleton pattern also has the judgment that the object exists. exists returns the object, does not exist, creates the object, and returns. Each time you create an instance object, there is a layer of judgment. The factory model is more about the problem of extended maintenance. In general, singleton patterns and factory models can produce more reasonable objects. How is it convenient to call these objects? And in the project so set up objects like stragglers, inconvenience management arrangements AH. Thus, the registration tree pattern emerges. Whether you're using a singleton or factory or a combination of both, all of them are "plugged in" to the registration tree. When I use an object, I just take it from the registration tree. This is as convenient and practical as we use global variables. And the registration tree mode provides a very good idea for other modes.

How do I implement a registration tree?

 With the above description, we seem to find a solution easily. First, we need a class as a registered tree, which is beyond doubt. All objects are "inserted" onto the registration tree. This registration tree should be played by a static variable. And this registration tree should be a two-dimensional array. This class should have a method (set ()) that inserts an object instance, and if so, there should be a way to undo the object instance (_unset ()). Of course, the most important thing is to have a method for reading the object (get ()). With these, we can happily complete the registration tree mode ~ ~ ~

Let's make a small combination of the three patterns below. Simply creating an instance object is far less complex, but in large projects, convenience is self-explanatory.

The registered tree mode of PHP design mode

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.