Concept
Proxy Pattern: an object structural pattern. Provide a proxy for an object, and the proxy object controls the reference to the original object.
UML
Role
Abstract Subject Role (Subject): Defines the public interfaces of RealSubject and Proxy, so that Proxy can be used wherever RealSubject is used.
Real Subject Role (RealSubject): Defines the real entity represented by Proxy.
Proxy object (Proxy): Saves a reference so that the proxy can access the entity, and provides an interface the same as the RealSubject interface, so that the proxy can be used instead of the entity (RealSubject).
Applicable scenarios
According to the purpose of using the proxy mode, common proxy modes have the following types:
Remote proxy: Provide a local proxy object for an object located in a different address space. This different The address space can be in the same host or another host. The remote agent is also called an Ambassador.
Virtual proxy: If you need to create an object that consumes a large amount of resources, first create an object that consumes a relatively small amount of resources to represent it. The real object will only be created when needed.
Copy-on-Write agent: It is a type of virtual agent that delays the copy (clone) operation until it is executed only when the client really needs it. Generally speaking, deep cloning of an object is an expensive operation. The Copy-on-Write proxy can delay this operation and the object will only be cloned when it is used.
Protect or Access agent: Controls access to an object and can provide different levels of usage permissions to different users.
Cache agent: Provides temporary storage space for the results of a certain target operation so that multiple clients can share these results.
Firewall (Firewall) proxy: Protect the target from malicious users.
Synchronization proxy: enables several users to use an object at the same time without conflict.
Smart Reference Agent: When an object is referenced, it provides some additional operations, such as recording the number of times this object has been called.
Code
The code is as follows:
<?php header("Content-type:text/html;Charset=utf-8"); /** * Interface Subject 抽象主题角色 * * 定义RealSubject和Proxy共同具备的东西 */ interface Subject { public function say(); public function run(); } /** * Class RealSubject 真正主题角色 */ class RealSubject implements Subject { // 姓名 private $_name; /** * RealSubject constructor. 构造方法 * * @param $name */ public function __construct($name) { $this->_name = $name; } /** * 说话 */ public function say() { echo $this->_name."在说话<br>"; } /** * 在跑步 */ public function run(){ echo $this->_name."在跑步<br>"; } } /** * Class Proxy 代理对象 */ class Proxy implements Subject { // 真实主题对象 private $_realSubject = null; /** * Proxy constructor. 构造方法,依赖注入方式储存真实对象 * * @param RealSubject|null $realSubject */ public function __construct(RealSubject $realSubject = null) { if (empty($realSubject)) { $this->_realSubject = new RealSubject(); } else { $this->_realSubject = $realSubject; } } /** * 调用说话方法 */ public function say() { $this->_realSubject->say(); } /** * 调用跑步方法 */ public function run() { $this->_realSubject->run(); } } /** * Class Client 本地测试 */ class Client { public static function test() { // 创建 $subject = new RealSubject("张三"); // 代理 $proxy = new Proxy($subject); // 张三在说话 $proxy->say(); // 张三在跑步 $proxy->run(); } } // 测试 Client::test();
Operating results:
Zhang San is talking
Zhang San is running
Advantages and Disadvantages
Advantages:
The proxy mode can coordinate the caller and the callee, at a certain time Reduces the coupling of the system to a certain extent.
Remote proxy allows the client to access objects on the remote machine. The remote machine may have better computing performance and processing speed and can quickly respond and handle client requests.
Virtual agents can reduce the consumption of system resources, optimize the system and increase running speed by using a small object to represent a large object.
Protection agents control access to real objects.
Disadvantages:
Due to the addition of a proxy object between the client and the real topic, some types of proxy modes may cause the request to be processed slower.
Implementing proxy patterns requires additional work, and the implementation of some proxy patterns is very complex.