重温 Thinking in Java 9 - Dynamic proxies

Proxy is one of the basic design patterns. It is an object that you insert in place of the "real" object in order to provide additional or different operations-these usually involve communication with a "real" object, so a proxy typically acts as a go-between.

 

interface Interface {
	void doSomething();
	void somethingElse(String args);
}

class RealObject implements Interface {
	public void doSomething(){
		System.out.println("doSomething");
	}
	
	public void somethingElse(String arg){
		System.out.println("somethingElse " + arg);
	}
}

class SimpleProxy implements Interface {
	private Interface proxied;
	
	public SimpleProxy(Interface proxied){
		this.proxied = proxied;
	}
	
	public void doSomething(){
		System.out.println("SimpleProxy doSomething");
		proxied.doSomething();
	}
	
	public void somethingElse(String arg){
		System.out.println("SimpleProxy somethingElse " + arg);
		proxied.somethingElse(arg);
	}
}

public class SimpleProxyDemo {
	public static void consumer(Interface iface){
		iface.doSomething();
		iface.somethingElse("bonobo");
	}
	
	public static void main(String[] args){
		consumer(new RealObject());
		consumer(new SimpleProxy(new RealObject()));
	}
}
/* Output:
doSomething
somethingElse bonobo
SimpleProxy doSomething
doSomething
SimpleProxy somethingElse bonobo
somethingElse bonobo

Because consumer() accepts an Interface, it can't know if it's getting a RealObject or a SimpleProxy, because both implement Interface.
But the SimpleProxy inserted between the client and the RealObject performs operations and then calls the identical method on 
a RealObject.
*/
 

A proxy can be helpful anytime you'd like to separate extra operations into a different place than the "real object," and especially when you want to easily change from not using the extra operations to using them, and vice versa (the point of design patterns is to encapsulate change-so you need to be changing things in order to justify the pattern). For example, what if you wanted to track calls to the mehtods in the RealObject, or to measure the overhead of such calls? This is not code you want to have incorporated in your application, so a proxy allows you to add and remove it easily.

Java's dynamic proxy takes the idea of a proxy one step further, by both creating the proxy object dynamically and handling calls to the proxied methods dynamically. All calls made on a dynamic proxy are redirected to a single invocation handler, which has the job of discovering what the call is and deciding what to do about it.

 

import java.lang.reflect.*;

class DynamicProxyHandler implements InvocationHandler {
	private Object proxied;
	
	public DynamicProxyHandler(Object proxied){
		this.proxied = proxied;
	}
	
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		//Things proxy can do
		System.out.println("**** proxy: " + proxy.getClass() + ", method: " + method + ", args: " + args);
		if(args != null){
			for(Object arg : args){
				System.out.println(" " + arg);
			}
		}
		//Things proxy can do end
		//Call real object method
		return method.invoke(proxied, args);
	}
}

public class SimpleDynamicProxy {
	public static void consumer(Interface iface){
		iface.doSomething();
		iface.somethingElse("bonobo");
	}
	
	public static void main(String[] args){
		RealObject real = new RealObject();
		consumer(real);
		//Insert a proxy and call again
		Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(),
						new Class[] { Interface.class },
						new DynamicProxyHandler(real));
		consumer(proxy);
	}
}
/* Output:
doSomething
somethingElse bonobo
**** proxy: class $Proxy0, method: public abstract void Interface.doSomething(), args: null
doSomething
**** proxy: class $Proxy0, method: public abstract void Interface.somethingElse(java.lang.String), args: [Ljava.lang.Object;@1fb8ee3 bonobo
somethingElse bonobo

You create a dynamic proxy by calling the static method Proxy.newProxyInstance(), which requires a class loader(you can generally just hand
it a class loader from an object that has already been loaded), a list of interfaces (not classes or abstract classes) that you wish the
proxy to implement, and an implementation of the interface InvocationHandler. The dynamic proxy will redirect all calls to the invocation handler,
so the constructor for the invocation handler is usually given the reference to the "real" object so that it can forward requests once it performs
its intermediary task.

The invoke() method is handed the proxy object, in case you need to distinguish where the request came from-but in may case you won't care.
However, be careful when calling methods on the proxy inside invoke(), because calls through the interface are redirected through the proxy.

In general you will perform the proxied operation and then use Method.invoke() to forward the request to the proxied object, passing the
necessary arguments. This may initially seem limiting, as if you can only perform generic operations. However, you can filter for certain
method calls, while passing others through:
*/

 

import java.lang.reflect.*;

class MethodSelector implements InvocationHandler {
	private Object proxied;
	
	public MethodSelector(Object proxied){
		this.proxied = proxied;
	}
	
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
		if(method.getName().equals("interesting"))
			System.out.println("Proxy detected the interesting method");
		return method.invoke(proxied, args);
	}
}

interface SomeMethods {
	void boring1();
	void boring2();
	void interesting(String arg);
	void boring3();
}

class Implementation implements SomeMethods {
	public void boring1(){
		System.out.println("boring1");
	}
	
	public void boring2(){
		System.out.println("boring2");
	}
	
	public void interesting(String arg){
		System.out.println("interesting " + arg);
	}
	
	public void boring3(){
		System.out.println("boring3");
	}
}

public class SelectingMethods {
	public static void main(String[] args){
		SomeMethods proxy = (SomeMethods) Proxy.newProxyInstance(
			SomeMethods.class.getClassLoader(),
			new Class[] { SomeMethods.class},
			new MethodSelector(new Implementation()));
		proxy.boring1();
		proxy.boring2();
		proxy.interesting("bonobo");
		proxy.boring3();
	}
}
/* Output:
boring1
boring2
Proxy detected the interesting method
interesting bonobo
boring3
*/
 

The dynamic proxy is not a tool that you'll use every day, but is can solve certain types of problems very nicely.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值