@Yano
2016-07-26T23:33:12.000000Z
字数 4216
阅读 2163
Java
A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes.
Use of the proxy can simply be forwarding to the real object, or can provide additional logic. In the proxy extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked. For the client, usage of a proxy object is similar to using the real object, because both implement the same interface.
Proxy pattern 是一种设计模式。
从广义上来说,代理是某个对象的接口的实现
。代理可以是任意接口的实现:一个网络连接,一个内存中大的对象,一个文件,或者是其他很难或者不能复制的资源。简而言之,客户端能够通过代理,访问真实的对象,也可以增加额外的逻辑。对于客户端而言,使用代理和使用真实的对象是一样的,因为两者都实现了相同的接口。
Image
public interface Image {
public void displayImage();
}
RealImage
public class RealImage implements Image {
private String filename;
public RealImage(String filename) {
this.filename = filename;
loadImageFromDisk();
}
private void loadImageFromDisk() {
System.out.println("Loading " + filename);
}
public void displayImage() {
System.out.println("Displaying " + filename);
}
}
ProxyImage
public class ProxyImage implements Image{
private String filename;
private Image image;
public ProxyImage(String filename){
this.filename=filename;
}
public void displayImage() {
if (image==null) {
image=new RealImage(filename);
}
image.displayImage();
}
}
Test
public class Test {
public static void main(String[] args) {
Image image1 = new ProxyImage("image1");
image1.displayImage();
Image image2 = new ProxyImage("image2");
image2.displayImage();
}
}
输出
Loading image1
Displaying image1
Loading image2
Displaying image2
InvocationHandler
is the interface implemented by the invocation handler of a proxy instance
.
Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.
Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.
To create a proxy for some interface Foo:
InvocationHandler handler = new MyInvocationHandler(...);
Class<?> proxyClass = Proxy.getProxyClass(Foo.class.getClassLoader(), Foo.class);
Foo f = (Foo) proxyClass.getConstructor(InvocationHandler.class).newInstance(handler);
or more simply:
Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(), new Class<?>[] { Foo.class }, handler);
A dynamic proxy class (simply referred to as a proxy class below) is a class that implements a list of interfaces specified at runtime when the class is created, with behavior as described below. A proxy interface is such an interface that is implemented by a proxy class. A proxy instance is an instance of a proxy class. Each proxy instance has an associated invocation handler object, which implements the interface InvocationHandler. A method invocation on a proxy instance through one of its proxy interfaces will be dispatched to the invoke method of the instance's invocation handler, passing the proxy instance, a java.lang.reflect.Method object identifying the method that was invoked, and an array of type Object containing the arguments. The invocation handler processes the encoded method invocation as appropriate and the result that it returns will be returned as the result of the method invocation on the proxy instance.
BookInterface接口,定义抽象方法addBook()。
public interface BookInterface {
public void addBook();
}
BookImpl类,实现了接口的addBook()方法。
public class BookImpl implements BookInterface {
public void addBook() {
System.out.println("addBook method");
}
}
BookProxy类实现了InvocationHandler接口。
/**
* JDK动态代理类
*
* @author Administrator
*
*/
public class BookProxy implements InvocationHandler {
private Object target;
/**
* 绑定委托对象,并返回一个代理类
* @param target
* @return
*/
public Object bind(Object target) {
this.target = target;
//取得代理对象,但是要绑定接口
return Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(), this);
}
/**
* 调用方法
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
result = method.invoke(target, args);
return result;
}
}
TestProxy
public class TestProxy {
public static void main(String[] args) {
// 生成一个动态代理类 proxy
BookProxy proxy = new BookProxy();
// 绑定接口,并返回动态代理 bookProxy
BookInterface bookProxy = (BookInterface) proxy.bind(new BookImpl());
bookProxy.addBook();
}
}