PHP design mode proxy agent mode

Source: Internet
Author: User
An agent refers to a role acting on behalf of another role, as in life, a wine manufacturer who is not directly selling wine to retail customers is through an agent to complete his sales business. and customers, do not need to drink red wine everywhere looking for factories, he just find the manufacturer in the local agent on the line, the specific wine factory there, customers do not care, the agent will help him deal with.
The proxy mode is to provide a proxy object to an object, and the proxy object controls the reference to the specific object.
Roles involved in proxy mode:
Abstract theme role, which declares the public interface of the proxy topic and the real topic, so that any place that needs a real theme can be replaced by a proxy theme.
The proxy theme role, which contains references to real-world topics, allows you to manipulate real-world themes at any time, providing the same interface as the real theme, so that it can replace real-world themes at any time. The proxy topic, by holding a reference to a real topic, can not only control the creation or deletion of real topics, but can intercept real-world topics before they are called, or perform certain operations after they are called.
A real proxy object that defines the specific object that the proxy role represents.
Refer to the code:

Copy the Code code as follows:


/**
* Proxy mode
*
* Provide an agent 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 ();


How the Proxy mode works: First, because both the agent theme and the real topic have implemented the common interface, which allows us to use the proxy theme instead of the original interface, as long as the real topic object is used. Second, the agent theme plays an intermediary role between the customer and the real topic, and with this intermediary platform, we can do the necessary preprocessing before passing the customer request to the real topic.
There is also a very common use example of proxy mode is the control of large image browsing. On our common website to browse the information of the text, do not know if you have noticed that the picture position is reduced, when someone to carefully view this picture, you can click on the image to activate a link, open a new page to see the picture. This is good for improving browsing speed, because not everyone is going to look at the information on the map carefully. This situation can be fully implemented using proxy mode. Here I will express the idea, as to the realization because of the work, do not express, as to this way in the B/s mode of the real feasibility, I have not confirmed, just out of thin imagination. If it is not feasible, then this example can be placed in a C/s down to achieve, this is absolutely no problem, and in many introduction design patterns of books and articles used. Two ways of achieving interest can be tried since:)
When we visit a Web page in a browser, it is not the actual method of loading the picture, but the method in the proxy object, in which a single thread is used to load a smaller version of the image into the browser, and another thread is used in the background to invoke the actual loading of the large picture into a local image. When you want to view this image, it will be displayed in the new page. Of course if the picture has not been loaded successfully when you want to browse, you can start a thread to display the prompt message until the load is successful.
This proxy mode function in the above embodiment of the incisively and vividly-through the agent to the real picture loading into the background to operate, so that it does not affect the foreground of the browser.
The proxy mode can coordinate the caller and the callee, and can reduce the coupling degree of the system to some extent. However, it is important to remember the above-mentioned conditions of using the proxy mode, otherwise the use of proxy mode will not have a good effect, and perhaps there will be problems.

The above describes the PHP design mode proxy agent mode, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.