代理(Proxy)模式

本文介绍了代理模式,包括远程代理、保护代理、虚拟代理和智能指引等类型,说明了各参与者(Proxy、Subject、RealSubject)的作用。还给出了Subject、RealSubject和Proxy的代码实现,展示了代理模式如何为其他对象提供代理以控制对该对象的访问。

为其他对象提供一种代理以控制对这个对象的访问。

远程代理(remote proxy)为一个对象在不同的地址空间提供局部代表。

保护代理(Protection proxy)控制对原始对象的访问。

虚拟代理(Virtual proxy)根据需要创建开销很大的对象。

智能指引(Smart Reference)取代简单的指针,它在访问对象时执行一些附加操作。包括对指向对象的引用计数,当第一次引用一个持久对象时,将它载入内存。

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 6in; HEIGHT: 222.75pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.png"></imagedata></shape>

参与者:

ProxyImageProxy

保存一个引用使得代理可以访问实体。若realSubjectSubject的接口相同,Proxy会引用Subject

提供一个与Subject的接口相同的接口,这样代理就可以用来代替实体。

控制对实体的存取,并可能负责创建和删除它。

其他功能依赖于代理的类型:

Remote proxy负责对请求及其参数进行编码,并向不同地址空间中的实体发送以编码的请求。

Virtual Proxy可以缓存实体的附加信息,以便延迟对它的访问。

Protection Proxy检查调用者是否具有实现一个请求所必需的访问权限。

SubjectGraphic

定义RealSubjectProxy的公用接口,这样就在任何使用RealSubject的地方都可以使用Proxy

RealSubjectImage

定义Proxy所代表的实体。

代码

Subject

/**

The Subject defines the common interface for RealSubject and Proxy so that a

* Proxy can be used anywhere a RealSubject is expected.

* @modelguid {F53E01CF-38BC-4532-8A7C-E49269EA26B0}

*/

public interface GraphicSubject {

/** @modelguid {3F5433C7-4967-4588-B350-A5B6089108EE} */

public void Draw();

/** @modelguid {98193698-60AB-44A5-9A90-80637E1171FF} */

public void Save();

/** @modelguid {5A8063C8-8849-4126-9A6B-676635E93F75} */

public void Load(Object o);

}

RealSubject

/**

The RealSubject defines the real object that the proxy represents.

* @modelguid {29F3D9A9-6FB1-4518-8B5C-B213F1E2FBE7}

*/

public class ImageRealSubject implements GraphicSubject {

/** @modelguid {408A6002-D939-4ACF-8D01-F694F69A5E11} */

public ImageRealSubject() {

}

/** @modelguid {A3D4D0AA-EDC1-468A-BFC1-195927B1FF5F} */

public void Draw() {

// add your codes here...

// be sure you update the return type based on your custom operation signature

System.out.println("Draw here");

return;

}

/** @modelguid {E3BF6A53-9276-47FB-AD54-639B29D260CD} */

public void Save(){

System.out.println("Save here");

}

/** @modelguid {FB73AB03-4D33-43E5-9031-5797878FBA83} */

public void Load(Object o){

System.out.println("Load here");

}

}

Proxy

/**

Proxy controls access to the real subject and may be responsible for creating

* and deleting it.

The Proxy maintains a reference that lets the proxy access the real subject.

* Proxy may refer to a Subject if the RealSubject and Subject interfaces are

* the same. It provides an interface identical to Subject's so that a proxy

* can be substituted for the real subject. It also controls access to the

* real subject and may be responsible for creating and deleting it.

Other responsibilities depend on the kind of proxy:

- remote proxies are responsible for encoding a request and its arguements and

* for sending the encoded request to the real subject in a different address

* space.

virtual proxies may cache additional informatin about the real subject so that

* they can postpone accessing it. For example, the ImageProxy for the

* Motivation caches the real image's extent.

- protection proxies check that the caller has the access permissions required

* to perform a request.

* @modelguid {1B76A4ED-C987-47A1-94B5-BDF5A3F19938}

*/

public class ImageProxy implements GraphicSubject {

/** @modelguid {4EE79008-471B-4A7E-AE49-16406E804CBB} */

private ImageRealSubject theRealSubject;

/**

This is the overloaded constructor of the Proxy class.

* @modelguid {7D49A0C9-F9D5-4C24-B6D9-99D667E46623}

*/

public ImageProxy(ImageRealSubject subject) {

this.theRealSubject = subject;

}

/**

This is the default constructor of the Proxy class.

* @modelguid {69DB530A-E37E-4DB6-AABC-6A96F1048363}

*/

public ImageProxy() {

}

/**

This operation is setting "theRealSubject" attribute of the Proxy class.

* @modelguid {E88BF991-2B8C-47B8-BA98-D65819F5B73F}

*/

public void setRealSubject(ImageRealSubject realSubject) {

theRealSubject = realSubject;

}

/**

This is any custom operation from the model. It can implement any logic

* required by your application.

* @modelguid {356AF68C-1BF2-4F8B-83D5-8DEC58BC2341}

*/

public void Draw() {

if (theRealSubject != null)

(theRealSubject).Draw();

}

/** @modelguid {1A9CDEDD-75E2-4C26-9272-C7203804CB73} */

public void Save(){

if (theRealSubject != null)

(theRealSubject).Save();

}

/** @modelguid {BB51C2C8-4302-4493-86DC-7D1F1BDD7DD5} */

public void Load(Object o){

if (theRealSubject != null)

(theRealSubject).Load(o);

}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值