王朝网络
分享
 
 
 

代理(Proxy)模式

王朝other·作者佚名  2006-02-01
宽屏版  字体: |||超大  

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

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

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

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

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

参与者:

Proxy(ImageProxy)

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

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

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

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

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

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

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

Subject(Graphic)

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

RealSubject(Image)

定义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);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有