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

Jul 21, 2016 pm 03:07 PM
php proxy acting Enhance deal with it pointer model go deep of program Simple parse Design Patterns

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles