为其他对象提供一种代理以控制对这个对象的访问。
远程代理(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);
}
}