代理模式的基本概念与工作原理
代理模式(Proxy)旨在增强对对象的引用机制,通常表现为对普通指针或句柄的替代。在此结构中,代理对象充当客户端与真实执行主体之间的中介层,其核心在于提供一个可被多方复用的钩子接口,从而接管原本直接指向目标的访问路径。
从技术架构来看,该模式在客户端与真实主体(RealSubject)之间介入一个代理层,严格遵循Subject接口规范,并将方法调用委派给不同逻辑处理。代理能够透明地执行多种辅助任务,例如惰性初始化RealSubject、数据预加载、跨机器消息交换或实施写时复制策略。这一机制类似于HTTP代理:浏览器等客户端依赖代理与服务器通信,代理在处理连接的同时,可额外承担访问控制或缓存大文件下载等功能。

尽管代理模式的对象图结构与装饰模式相似,但二者设计初衷截然不同。装饰模式侧重于动态地为对象增加新功能行为,而代理模式的核心目的在于控制对客户端的访问权限。此外,代理模式的一个显著特征是按需创建RealSubject实例,仅在真正需要时才进行实例化操作。
该模式主要包含四个参与者: ◆客户端(Client):仅依赖于Subject接口的实现; ◆主体(Subject):作为RealSubject的抽象定义; ◆真实主体(RealSubject):负责执行高成本计算或管理海量数据; ◆代理(Proxy):向Client提供与Subject一致的引用,仅在必要时才创建或与RealSubject实例建立通信。
以下是两个广泛应用的代理模式场景: 1、对象-关系映射(ORMs)在运行时生成代理类,作为实体类的子类以实现懒加载(即虚拟代理)。此类代理会重写所有实体方法,并在方法实际调用前插入数据载入逻辑,确保数据未被请求时不占用内存。ORM代理支持对象间的双向关联,且无需加载整个数据库,因为它们位于当前加载对象图的边界处。
2、Java RMI利用远程代理对象(远程代理)处理分布式调用。当代理方法被触发时,它会序列化参数并发起网络请求,将调用委托给另一节点上的真实对象。这种技术实现了远程调用的透明性,开发者无需关心对象是否位于同一台机器,但需注意这种透明度可能导致执行效率降低。
以下代码示例展示了ImageProxy的实现,旨在推迟图像数据的加载过程。 ```php /** * 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 ``` 上述代码演示了PHP中的代理模式应用。简而言之,代理模式通过为其他对象提供一个代理来控制对该对象的访问。