代理模式总结
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">代理模式是对象的结构模式。代理模式给某一个对象提供一个代理对象,并由代理对象控制原对象的引用。</p>
<h1 style="text-align: left; margin-bottom: 10px;">一、代理的种类</h1>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">* <strong style="color: blue;">远程(Remote)代理</strong>:为一个位于不同的地址空间的对象提供一个局域代表对象。这个不同的地址空间可以是在本地机器中,也可以是在另一台机器中,远程代理又叫做大使(Ambassador)。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">* <strong style="color: blue;">虚拟(Virtual)代理</strong>:根据需要创建一个资源消耗较大的对象,使得此对象只在需要时才会被真正创建。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">* <strong style="color: blue;">Copy-on-Write代理</strong>:虚拟代理的一种,把复制拖延到只有在客户端需要时,才真正采取行动。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">* <strong style="color: blue;">保护(Protect or Access)代理</strong>:控制对一个对象的访问,如果需要,可以给不同的用户提供不同级别的使用权限。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">* <strong style="color: blue;">Cache代理</strong>:为某一个目标操作的结果提供临时的存储空间,以便多个客户端可以共享这些数据。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">* <strong style="color: blue;">防火墙(Firewall)代理</strong>:保护目标,不让恶意用户接近。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><strong style="color: blue;">二、代理模式的结构</strong></p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">代理模式涉及的角色有:</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">* <strong style="color: blue;">抽象主题角色</strong>:声明了真实主题和代理主题的共同接口,这样一来在任何可以使用真实主题的地方都可以使用代理主题。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">* <strong style="color: blue;">代理主题(Proxy)角色</strong>:代理主题角色内部含有对真实主题的引用,从而可以在任何时候操作真实主题对象;代理主题角色提供了一个与真实主题角色相同的接口,以便可以再任何时候都可以替代真实主题;控制对真实主题的引用,负责在需要的时候创建真实主题对象;代理角色通常在讲客户端调用传递给真实的主题之前或者之后,都要执行某个操作,而不是单纯地将调用传递给真实主题对象。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">* <strong style="color: blue;">真实主题角色</strong>:定义了代理角色所代表的真实对象。</p>
<h1 style="text-align: left; margin-bottom: 10px;">三、java对代理模式的支持</h1>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">java语言通过在java.lang.reflect库中提供下面三个类直接支持代理模式:Proxy,InvocationHandler和Method。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">其中Proxy类使得设计师能够在运行时间创建代理对象。当系统有了一个代理对象后,对原对象的方法调用会首先被分派到一个调用处理器(Invocation Handler).</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">程序可以在调用处理器的invoke()方法中截获这个调用,进行额外的操作。显然,java所提供的这一支持是建立在反射(reflection)的基础之上的。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">设计师可以按照下面的步骤创建动态代理对象:</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">*指明一系列的接口来创建一个代理对象。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">*创建一个调用处理器(Invocation Handler)对象。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">*将这个代理指定为某个其他对象的代理对象。</p>
<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">*在调用处理器的invoke()方法中采取代理,一方面将调用传递给真实对象,另一方面执行各种需要做的操作。</p>
<div style="text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/931b95a502fa4ac2af20b591a6ff4998~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1704566366&x-signature=aWP3B2wWvY9kRG5KWmFdbgJqbu4%3D" style="width: 100%; margin-bottom: 20px;"></div>
页:
[1]