Home > Backend Development > PHP Tutorial > An in-depth analysis of the proxy pattern of PHP design patterns_PHP tutorial

An in-depth analysis of the proxy pattern of PHP design patterns_PHP tutorial

WBOY
Release: 2016-07-21 15:07:10
Original
879 people have browsed it

Proxy mode (Proxy), which is an enhancement to a simple handler (or pointer), is used to reference an object: this pointer is replaced by a proxy (Proxy) object, which is located between the client and the real executor In between, pointers have a hook that can be exploited by multiple targets.

Technically speaking, this pattern inserts a proxy object between the client and the real subject (RealSubject), maintaining the subject interface and delegating its methods in different ways. Agents can do everything transparently: lazily create RealSubjects or load data, exchange messages with other machines, copy-on-write policies, etc. This is somewhat similar to an HTTP proxy, where clients (such as browsers) and applications rely on contact with the HTTP server. The proxy can complete other tasks while managing the connection, such as access control and caching large download files.


The object graph of the proxy mode is similar in structure to the object graph of the decoration mode, but the purpose of expression is different. The decorator dynamically adds behavior to the object, while the proxy controls access from the client. Additionally, the agent only creates RealSubjects when needed.

Participants:
◆Client: depends on the subject implementation;
◆Subject: abstraction of RealSubject;
◆RealSubject: Complete costly work or contain a large amount of data;
◆Proxy (Proxy): Provide a consistent reference to the Subject for the Client, and only create a RealSubject instance or interact with the RealSubject when needed Instance communication.

The following are two widely used examples of proxy patterns:
1. Object-relational mapping (Orms) creates proxies as subclasses of entity classes at runtime , to implement lazy loading (virtual proxy), this proxy will cover all entity methods, append a loader in front, and will not contain any data before the method is actually called. Orms proxy supports bidirectional relationships between objects, without loading the entire Databases because they are placed at the boundaries of the currently loaded object graph.

2. Java RMI uses remote proxy objects (remote proxies). When their methods are called, the proxy serializes the parameters, performs the request on the network, and delegates the call to the real object on another node. This technology allows transparency You can call remote objects without worrying about whether they are on the same machine, but this transparency can easily slow down execution.

The following code example implements an ImageProxy to defer the loading of image data.

Copy code The code is as follows:

/** 
 * Subject interface. 
 * Client depends only on this abstraction. 
 */
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 without 
 * instancing the RealSubject are present here. 
 */
abstract class AbstractImage 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 AbstractImage 

    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 AbstractImage 

    public function __construct($path) 
    { 
 $this->_path = $path; 
 list ($this->_width, $this->_height) = getimagesize($path); 
    } 

    /** 
     * Creates a RawImage and exploits its 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 would 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 PHP’s proxy mode. Simply put, the proxy pattern is to provide a proxy for other objects to control access to this object.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327556.htmlTechArticleProxy mode (Proxy), which is an enhancement to a simple handler (or pointer), used to reference an object : This pointer is replaced by the Proxy object, which is located on the client (...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template