PHP Registration number Mode

Source: Internet
Author: User

After the first two singleton and factory models, we finally ushered in the last basic design pattern-the registered tree Mode.

 What is a registered tree mode?

Registration tree mode is also called Registration mode, Registrar mode. The reason I am here to make a sentimental name is because I feel 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 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.

<?Php
Create a single caseClasssingle{Public$hash;StaticProtected$ins =Null;FinalProtectedfunction__construct () {$this->hash=Rand (1,9999); }StaticPublicfunctionGetInstance () {If (self::$insinstanceof Self) {Return self::$ins; } self::$ins =NewSelf ();Return self::$ins; }}//factory modeClassrandfactory{PublicStaticfunctionFactory () {Return single::GetInstance (); }}//registration TreeClassregister{ProtectedStatic$objects;PublicStaticfunction set ($alias,$object) {self::$objects [$alias]=$object; }PublicStaticfunction 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, three modes of the design are Complete. The pattern design itself will complement each other, and when it comes to other patterns, one or more of the other design patterns will be used.

A mode does not matter, I believe that the depth of programming, will produce a sudden sense of surprise, is willing to gentlemen and I progress together.

PHP Registration number 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.