In-depth analysis of the proxy mode in PHP design mode _ PHP Tutorial

Source: Internet
Author: User
In-depth analysis of the proxy mode in PHP design mode. Proxy mode, which is an enhancement to a simple handler (or pointer) and is used to reference an object: this pointer is replaced by a Proxy object, the Proxy object is located on the client (Proxy mode). It is an enhancement to a simple handler (or pointer) and is used to reference an object: this pointer is replaced by a Proxy object, the proxy object is located between the Client and the real execution program, and the pointer has a hook that can be used by multiple targets.

Technically speaking, this mode inserts a proxy object between the client and the real entity (RealSubject), maintains the subject interface, and delegates its methods in different ways. The proxy can do anything transparently: lazy create RealSubject or load data, exchange messages with other machines, and copy policies when writing. This is similar to the HTTP proxy. its clients (such as browsers) and applications depend on the connection with the HTTP server. the proxy can complete other tasks during connection management, such as access control and cache of large-sized download files.


The object graph in proxy mode is similar to the object graph in decoration mode in structure, but its purpose is different. the modifier dynamically adds behavior to the object, while the proxy controls access from the client. In addition, the proxy creates RealSubject only when necessary.

Participants:
◆ Client: depends on the implementation of the Subject (Subject;
◆ Subject: abstraction of RealSubject;
◆ RealSubject: completes costly work or contains a large amount of data;
◆ Proxy: provides the Client with a reference consistent with that of Subject. you can create a RealSubject instance or communicate with a RealSubject instance only when necessary.

The following are two examples of widely used proxy modes:
1. object-relational ing (Orms) creates a proxy as a subclass of the object class in the running state to implement lazy loading (virtual proxy). This proxy will overwrite all object methods, append a loader before, without including any data before the method is actually called. The Orms agent supports bidirectional relationships between objects and does not need to load the entire database, because they are placed at the boundary of the currently loaded object graph.

2. Java RMI uses remote proxy objects (remote proxies). when their methods are called, they serialize parameters, execute network requests, and Delegate calls to real objects on another node, this technology allows transparent calls to remote objects without worrying about whether they are on the same machine, but this transparency can easily slow down execution.

The following sample code implements an ImageProxy that delays image data loading.

The code is as follows:


/**
* Subject interface.
* Client depends only on this operation action.
*/
Interface Image
{
Public function getWidth ();

Public function getHeight ();

Public function getPath ();

/**
* @ Return string the image's byte stream
*/
Public function dump ();
}

/**
* Abstract class to avoid repetition of boilerplate code in the Proxy
* And in the Subject. Only the methods which can be provided
* Instancing the RealSubject are present here.
*/
Abstract class implements actimage implements Image
{
Protected $ _ width;
Protected $ _ height;
Protected $ _ path;
Protected $ _ data;

Public function getWidth ()
{
Return $ this-> _ width;
}

Public function getHeight ()
{
Return $ this-> _ height;
}

Public function getPath ()
{
Return $ this-> _ path;
}
}

/**
* The RealSubject. Always loads the image, even if no dump of the data
* Is required.
*/
Class RawImage extends actimage
{
Public function _ construct ($ path)
{
$ This-> _ path = $ path;
List ($ this-> _ width, $ this-> _ height) = getimagesize ($ path );
$ This-> _ data = file_get_contents ($ path );
}

Public function dump ()
{
Return $ this-> _ data;
}
}

/**
* Proxy. Defers loading the image data until it becomes really mandatory.
* This class does its best to postpone the very expensive operations
* Such as the actual loading of the BLOB.
*/
Class ImageProxy extends actimage
{
Public function _ construct ($ path)
{
$ This-> _ path = $ path;
List ($ this-> _ width, $ this-> _ height) = getimagesize ($ path );
}

/**
* Creates a RawImage and exploits functionalities.
*/
Protected function _ lazyLoad ()
{
If ($ this-> _ realImage === null ){
$ This-> _ realImage = new RawImage ($ this-> _ path );
}
}

Public function dump ()
{
$ This-> _ lazyLoad ();
Return $ this-> _ realImage-> dump ();
}
}

/**
* Client class that does not use the data dump of the image.
* Passing blindly a Proxy to this class and to other Clients makes sense
* As the data wocould be loaded anyway when Image: dump () is called.
*/
Class Client
{
Public function tag (Image $ img)
{
Return ';
}
}

$ Path = '/home/giorgio/shared/Immagini/kiki.png ';
$ Client = new Client ();

$ Image = new RawImage ($ path); // loading of the BLOB takes place
Echo $ client-> tag ($ image), "\ n ";

$ Proxy = new ImageProxy ($ path );
Echo $ client-> tag ($ proxy), "\ n"; // loading does not take place even here


The above code implements the PHP proxy mode. In short, the proxy mode provides a proxy for other objects to control access to this object.

Proxy, which is an enhancement to a simple handler (or pointer) and is used to reference an object: this pointer is replaced by a Proxy object, the proxy object is located on the client (...

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.