Php pattern design-registration tree pattern-php Tutorial

Source: Internet
Author: User
Php pattern design: the registration tree pattern has finally ushered in the last Basic design pattern-registration tree pattern after the first two Singleton patterns and factory patterns.

 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. Therefore, the registration tree model emerged. 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 the corresponding method is set, there should be a method to unset the object instance (_ unset ()). Of course, the most important thing is to have a get () method to read objects ()). 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.

 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.

   

Series of articles:

Php pattern design-Singleton pattern

    Factory model of php pattern design

   Php pattern design-registration tree pattern

  

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.