PHP design pattern: Registration tree pattern analysis, php design pattern tree pattern

Source: Internet
Author: User

PHP design pattern: Registration tree pattern analysis, php design pattern tree pattern

This example describes the PHP registration tree mode. We will share this with you for your reference. The details are as follows:

What is the registration tree model?

The registration tree mode is also called the registration mode. The reason why I am here is that the name of the registration tree is easier to understand. Like the first two articles, we still start with the name. The registration tree mode registers an object instance to a global object tree. The Pattern Design Method for picking from the object tree is required. This reminds me of the fact that when I was a child, I bought a sugar gourd. When I sold it, I inserted it on a large pole. People took it out when they bought it. The difference is that the registration tree model will be picked up many times, and the sugar gourd will not be picked once...

Why use the registration tree mode?

The Singleton mode solves the problem of creating a unique object instance in the entire project, and the factory mode solves the problem of creating an instance object through the new method. What problems does the registration tree solution solve? Before considering this problem, we still need to consider the limitations currently faced by the first two models. First, the process of creating a unique object in singleton mode also determines whether the object exists. If yes, an object is returned. If no, an object is created and a result is returned. This layer of judgment is required for each instance object creation. The factory model is more concerned with extended maintenance. In general, the singleton mode and the factory mode can produce more reasonable objects. How can we conveniently call these objects? In addition, it is difficult to make overall management arrangements for the objects set up in the project as if they were scattered. Because of this, the registration tree model came into being. Whether you are using the singleton mode, the factory mode, or the objects generated by the combination of the two, all of them are inserted to the registration tree. When I use an object, just retrieve it from the registration tree. This is as convenient and practical as using global variables. The registration tree model also provides a very good idea for other models.

How to Implement the registration tree?

Through the above description, it seems that we can easily find a solution. First, we need a class as the registration tree, which is beyond doubt. All objects are "inserted" to the registration tree. This registration tree should be acted by a static variable. The registration tree should be a two-dimensional array. This class should have a method to insert an object instance (set()), When making the corresponding method, there should be a method for revoking the object instance (_unset()). Of course, the most important thing is to have a method to read objects (get()). With this, we can happily complete the registration tree model ~~~

Let's make a small combination of the three modes. Creating an instance object alone is far from that complex, but the convenience is self-evident if it is used in large projects.

<? Php // create a singleton class Single {public $ hash; static protected $ ins = null; final protected function _ construct () {$ this-> hash = rand ();} static public function getInstance () {if (self: $ ins instanceof self) {return self: $ ins ;} self: $ ins = new self (); return self: $ ins ;}// factory mode class RandFactory {public static function factory () {return Single :: getInstance () ;}// Register 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 );

So far, the three modes have been introduced. The design of various models will complement each other. When we introduce other models, one or more other design modes will be used more or less.

It doesn't matter if you don't understand a model. I believe that the depth of programming will surely bring a sense of surprise. I hope you can make progress together with me.

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.