Php design mode Proxy (Proxy mode) _ PHP Tutorial

Source: Internet
Author: User
Php design mode Proxy (Proxy mode ). Proxy refers to a role that represents another role to take action. just like in life, a wine manufacturer does not directly sell customers of wine, but all of them are represented by agents, this means that one role represents another role to take action. in life, a wine manufacturer does not directly sell customers of red wine retail through agents. The customer does not need to look for factories to drink red wine. he only needs to find the agent of the manufacturer in the local area. The specific wine factory is there, and the customer does not need to care about it. the agent will help him deal with it.

The proxy mode provides a proxy object for an object, and the proxy object controls the reference of a specific object.

Roles involved in proxy mode:

Abstract The topic role and declare the public interface of the proxy topic and the real topic, so that any place that requires the real topic can be replaced by the proxy topic.
The proxy topic role contains the reference of a real topic, so that you can operate on the real topic at any time. the proxy topic has provided the same interface as the real topic, so that it can replace the real topic at any time. By referencing a real topic, a proxy topic can not only control the creation or deletion of a real topic, but also intercept a real topic before it is called or perform some operations after it is called.
Real proxy object, which defines the specific objects represented by the proxy role.
Refer to the code below:

The code is as follows:


/**
* Proxy mode
*
* Provides a proxy for other objects to control access to this object.
*
*/
Interface Proxy
{
Public function request ();
Public function display ();
}

Class RealSubject
{
Public function request ()
{
Echo "RealSubject request
";
}

Public function display ()
{
Echo "RealSubject display
";
}
}

Class ProxySubject
{
Private $ _ subject = null;
Public function _ construct ()
{
$ This-> _ subject = new RealSubject ();
}

Public function request ()
{
$ This-> _ subject-> request ();
}

Public function display ()
{
$ This-> _ subject-> display ();
}
}

$ ObjProxy = new ProxySubject ();
$ ObjProxy-> request ();
$ ObjProxy-> display ();


Proxy mode: first, because both the proxy topic and the real topic implement a common interface, so that we can avoid changing the original interface, proxy themes can be used as a place where real theme objects are used. Second, the proxy topic acts as an intermediary between the customer and the real theme. using this intermediary platform, we can make some necessary preprocessing before passing customer requests to the real theme.

Another common example of proxy mode is to control large image browsing. When browsing text and text information on a common website, I wonder if you have noticed that the image is placed in a narrow position. When someone wants to carefully view the image, you can click an image to activate a link to open the desired image on a new webpage. This is very good for improving the browsing speed, because not everyone has to look at the information on the picture carefully. In this case, you can use the proxy mode to fully implement it. I will express my ideas here. I will not express the implementation for work reasons. As for the practical feasibility of this method in the B/S mode, I have not confirmed it, just imagine it out of thin air. If it is not feasible, the example can be implemented in a C/S, which is absolutely no problem and used in many books and articles about the design pattern. If you are interested in the implementation of the two methods, please try again :)

When accessing a webpage in a browser, we call methods in the proxy object instead of loading images. in this object, first, use a thread to load a thumbnail image to the browser, and then use another thread in the background to call the real method of loading large images to load the image locally, when you want to browse this image, display it on the new page. Of course, if the image has not been loaded successfully when you want to browse, you can start another thread to display the prompt information until it is loaded successfully.

In this way, the proxy mode is fully embodied in the above-the real image is loaded to the background through the proxy, so that it does not affect the foreground browsing.

The agent mode can coordinate callers and callers, and reduce the coupling degree of the system to a certain extent. However, you must remember the conditions for using the proxy mode mentioned above. otherwise, using the proxy mode will not have good results, but may have problems.

...

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.