In-depth analysis of proxy mode in PHP design mode

Source: Internet
Author: User
This article provides a detailed analysis of the proxy mode in the PHP design mode. For more information, see

This article provides a detailed analysis of the proxy mode in the PHP design mode. For more information, see

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 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 a bit similar to HTTP proxy. Its clients (such as browsers) and applications depend on the connection with the HTTP server. The website space and proxy can complete other tasks when managing connections, 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 the Subject. On a Hong Kong virtual host, 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, the Hong Kong server leasing technology allows transparent remote object calls without worrying about whether they are on the same machine. However, 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.

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.