PHP design mode proxy (proxy mode) _php technique

Source: Internet
Author: User
Agent refers to the role of a character acting on behalf of another, like in life, a wine manufacturer, is not directly to the red wine retail customers, are through the agency to complete his sales business. And customers, also do not have to drink red wine and everywhere looking for factories, he just find manufacturers in the local agent on the line, the specific wine factory there, customers do not care, agents will help him deal with.

The proxy mode is to provide a proxy object to an object, and the proxy object controls the reference of the specific object.

Roles involved in agent mode:

Abstract theme roles, which declare a common interface between the agent theme and the real topic, so that any place that needs a real topic can be replaced with a proxy theme.
Agent theme role, contains the real subject of the reference, so that you can operate the real subject at any time, the agent theme of the pros and the real subject of the same interface, so that it can replace the real theme at any time. By holding a reference to the real topic, the agent theme can not only control the creation or deletion of the real topic, but also intercept the real topic before it is invoked, or do some action after the call.
A real proxy object that defines the specific object that the agent role represents.
Refer to the code:

Copy Code code as follows:

<?php
/**
* Agent Mode
*
* Provide 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<br/>";
}

Public Function display ()
{
echo "Realsubject display<br/>";
}
}

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 ();

How the agent mode works: First of all, because the agent theme and the real theme have implemented a common interface, which allows us to do not change the original interface, as long as the real subject object, can be replaced by the agent theme. Second, the agent theme plays a mediating role between the customer and the real subject, and by using this intermediary platform, we can do some necessary preprocessing before passing the customer request to the real topic.

There is also a common use example of proxy mode is the control of large picture browsing. In our common website to browse the text of the information, do not know if you have noticed that the picture position is narrowed, when someone to carefully view this picture, you can click on the image to activate a link, in a new page open to see the picture. This is good for improving browsing speed because not everyone is going to look at the information on the map. This situation can be fully implemented using proxy mode. Here I will express the idea, as for the realization because of the work reason, do not express, as for this way in B/s mode of the real feasibility, I did not confirm, just out of thin air imagination. If it is not feasible, then this example can be put into a C/s to achieve, this is absolutely no problem, but also in many of the introduction of design patterns of books and articles used. Two ways of achieving an interest can be attempted since:

When we visit a Web page in a browser, we call a method that does not actually load the picture. Instead, the method in the proxy object, in which a single thread is used to load a smaller version of the picture into the browser, and a different thread is used in the background to load the picture into the local, using a method to invoke the actual loading of the larger picture. When you want to browse the image, display it in the new page. Of course, if you want to browse when the picture has not been loaded successfully, you can start a thread to display the message until the load succeeds.

In this way, the function of the proxy mode is reflected in the above--through the agent to the real picture loaded into the background to operate, so that it does not affect the foreground browsing.

The agent mode can coordinate the caller and the callee, and can reduce the coupling degree of the system to some extent. However, be sure to remember the previous use of proxy mode conditions, otherwise the use of proxy mode will not have good results, perhaps there will be 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.