Home  >  Article  >  Backend Development  >  PHP design pattern Proxy proxy mode

PHP design pattern Proxy proxy mode

WBOY
WBOYOriginal
2016-07-29 08:45:48907browse

Agent refers to a character taking action on behalf of another character. Just like in life, a wine manufacturer will not directly sell wine to retail customers, but will complete its sales through an agent. As for the customer, he does not need to look for factories everywhere in order to drink red wine. He only needs to find the local agent of the manufacturer. The customer does not need to care about the specific location of the red wine factory, the agent will handle it for him.
The proxy mode is to provide a proxy object for a certain object, and the proxy object controls the reference of the specific object.
Roles involved in the proxy pattern:
Abstract theme role declares the public interface between the proxy theme and the real theme, so that any place that requires a real theme can be replaced by a proxy theme.
The proxy theme role contains a reference to the real theme, so that the real theme can be operated at any time. The proxy theme function provides the same interface as the real theme, so that it can replace the real theme at any time. By holding a reference to the real topic, the proxy topic can not only control the creation or deletion of the real topic, but also intercept the real topic before it is called, or perform certain operations after the call.
The real proxy object defines the specific object represented by the proxy role.
Refer to the code:

Copy the code The code is as follows:


/**
* Proxy pattern
*
* 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
";
}
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 proxy subject and the real subject implement a common interface, this allows us to do it without changing the original In the case of interfaces, proxy themes can be used instead wherever real theme objects are used. Secondly, the proxy topic plays an intermediary role between the client and the real topic. Using this intermediary platform, we can do some necessary preprocessing before passing the customer request to the real topic.
Another very common example of using the proxy mode is the control of large image browsing. When browsing graphic information on our common websites, I wonder if you have noticed that the position of the picture has been reduced. When someone wants to view the picture carefully, they can activate a link by clicking on the picture. A new web page opens to view the image. This is very good for improving browsing speed, because not everyone has to look at the information on the detailed image. This situation can be fully realized using the proxy mode. Here I will express my ideas. As for the implementation due to work reasons, I will not express it. As for the real feasibility of this method in B/S mode, I have not confirmed it, it is just my imagination. If this is not a feasible method, then this example can be implemented on a C/S. This is absolutely no problem and is used in many books and articles introducing design patterns. If you are interested in the implementation of the two methods, you can try it:)
When we access the web page in the browser, we call not the actual method of loading images, but the method in the proxy object. In this object, first use One thread loads a reduced version of the image to the browser, and another thread is used in the background to call the actual method of loading the large image to load the image locally. When you want to browse this image, load it on a new web page. displayed in . Of course, if the image has not been loaded successfully when you want to browse, you can start another thread to display the prompt message until the image is loaded successfully.
In this way, the function of the proxy mode is fully reflected in the above - the loading of real pictures is placed in the background through the proxy, so that it does not affect the browsing in the foreground.
The proxy mode can coordinate the caller and the callee, and can reduce the coupling of the system to a certain extent. However, you must remember the conditions for using the proxy mode mentioned earlier. Otherwise, using the proxy mode will not only not have good results, but may also cause problems.

The above introduces the PHP design pattern Proxy proxy mode, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn