Design Pattern I will talk about registers (registration pattern)

Source: Internet
Author: User
Design Pattern I will talk about registers (registration pattern)

Recently, a friend told me about registers or registration modes, and another friend gave me an address,
Let me check it out. After reading it, I was very confused about the PHP running method and began to wonder if I was
In many cases, I got a wrong understanding. Then I went to verify and asked some friends. Some of them also did tests to prove that I was correct,
As a result, many of my friends may have been excited after reading these design patterns. They discovered that they would use this pattern, and then they would get it from the PHP project.
First, I want to show my attitude. I want PHP oo. But due to all the shortcomings of PHP, PHP is not suitable for oo very thoroughly, so I feel
PHP programmers should take into account the reuse of extended Code while taking care of the efficiency. do not inherit too much, use too many classes, and add too many layers to solve some problems, all of these are my own views on PHP oo. Let's talk about dianzheng's

If you have time, I will give you two URLs.
Http://blog.phpsoho.com/read.php/80.htm
There is another
Http://blog.phpsoho.com/read.php/81.htm
After reading the two, you will be able to better understand the registration mode. After reading the two, do you especially want to apply this design mode in your own program? Yes, I think, because this is a good thing.

Let's talk about some other variables, classes, and so on in PHP. Where is his valid range? It's a page, or before HTML is generated, after HTML is generated, these variables become invalid. At least they cannot be changed, and their next page cannot be used.
So we declare a class in a page, call this page, and the class is instantiated, So If concurrent requests are sent to the same page, can this be instantiated by other pages, the answer is no, so the next time you open this page, in this new page, can this be used without being instantiated? The answer is no.

Let's talk about the advantages of the registration mode: many people may know that its advantage is to reduce the multiple declarations of classes and create a class pool or object pool, what do you need to get directly? If no declaration is made, I will declare one for you. If so, I will give you a declaration, which reduces the number of declarations of this class, reduces memory usage and improves program execution efficiency.

Well, now let's make a hypothesis that we only connect to the database once to execute an SQL statement on a page, that's enough. We have blocked the database as a database operation class. At this time, we directly use

Code: [copy to clipboard] <? PHP
$ Db = new mysqldb ();
$ Result = $ db-> query ("select * from table ");
?>

If we use the registration Mode

Code: [copy to clipboard] $ registry = new Registry ();
$ Registry-> set ('db', $ dB );
$ Registry-> set ('action', 'query ');
$ Result = $ registry-> get ('SQL', 'select * From table ');

Now let's talk about the basic ideas of the Registry class. I don't write a specific implementation, but let everyone understand the benefits.

Code: [copy to clipboard] <? PHP
Class Registry
{
VaR $ _ OBJECT = array ();
Function set ($ name, $ object)
{
$ This-> _ object [$ name] = $ object;
}
Fnction get ($ name)
{
Required urn $ this-> _ object [$ name];
}
}
?>

Now let's talk about it. When we don't use the registration mode, we can simply use the database manipulation class.
Now there is a premise that we only manipulate the data once
So we need to declare a class for this registration mode. Then we need to add more operations. To be honest, these operations are unnecessary. In this case, it is a waste to use the registration mode.

So let's talk about it again. Such a classic model has a classic application in other languages. If people have been using it for so long, the benefits will be lost.
Well, let me tell you this question. If we want to perform many operations on the data, at the same time, when we closed the method, each of our methods was independent, then we have many classes, and we need to declare them again. At this time, we can use the registration mode to experience the benefits of this classic design mode.
This can be extended. In ASP, it should be the same as in JSP, but in J2EE, it is different from other programs. I understand it in this way, I think Java's J2EE JSP only completes a presentation layer, and some of the actual things are completed by Java, java, however, is relatively complex. Its variables and resources are stored in the memory. This page is not closed and disappears. Therefore, this design mode is under development of J2EE, it is reasonable to use it. Of course, it also includes other languages. In a language like PHP, you should properly consider whether such overhead is worthwhile and necessary, the same applies to other design patterns.
Please make a brick.

Author: sanshi0815
Mail: sanshi0815@tom.com

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.