PHP design pattern Proxy (proxy mode)_PHP tutorial
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 mode:
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:
/ **
* 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 theme and the real theme implement a common interface, this allows us to use the proxy theme wherever the real theme object is used without changing the original interface. to replace. 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 to control the browsing of large images. When browsing picture and text 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. I will express my ideas here. As for the implementation, I will not express it due to work reasons. As for the real feasibility of this method in the 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 out:)
When we access the web page in the browser, what we call is not the actual method of loading images, but the method in the proxy object. In this In the object, a thread is first used to load 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, you will It is displayed in a new web page. 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 - through the proxy, the loading of real pictures is placed in the background for operation, 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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
