Java proxy

Java proxy

 

proxy, a design pattern base on reflect, it handle method invocation on object with a proxy instance,

 

------

class & interface

 

include:

* java.lang.reflect.Proxy

help to create proxy instance,

* java.lang.reflect.InvocationHandler

implement it to specify the proxy logic,

 

------

create & use a proxy

 

steps:

* pre-step:

* create the proxyed interface

* create the proxyed class implements the proxyed interface

* create a class implement InvocateHandler,

do:

* provide a field which is the proxyed object,

* provide a bind method(e.g. bind()), pass the proxyed object as param, and call Proxy.newProxyInstance() to create & return a proxy instance,

* implement the invoke() method, it call the method and return the return value, also do some other logic as you wish,

* get proxy instance

create an instance of the class that implement InvocateHandler,

and call the bind method with a proxyed object as param, to get a proxy instance,

cast the proxy instance to type that is one of the proxyed object's interface,

* call method with proxy instance

just the same as call on the proxyed object,

 

------

proxy & interface

 

proxy instance must be cast to an interface type, but not the class itself or it's superclass,

and all method to be invode via proxy, must be declared in the interface,

 

this is because Proxy.newProxyInstance's second param accept only interface, typically we use Class.getInterfaces() to pass the param,


------

code


IHello.java

 

package eric.j2se.proxy;

/**
 * interface for proxyed class
 * 
 * @author eric
 * @date Dec 7, 2012 6:07:36 PM
 */
public interface IHello {
	public void hello(String name);
}
 

 

Hello.java

 

package eric.j2se.proxy;

/**
 * proxyed class
 * 
 * @author eric
 * @date Dec 7, 2012 6:07:49 PM
 */
public class Hello implements IHello {
	@Override
	public void hello(String name) {
		System.out.printf("hello, %s!\n", name);
	}
}
 

 

TimerProxy.java

 

package eric.j2se.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.apache.log4j.Logger;

/**
 * a proxy that help to logging method execution time
 * <p>
 * <b>how to use</b>:
 * <p>
 * <b>way one</b>: create an instance of this class, and call bind() with the object to be proxyed, and get a proxy instance, cast the return value of bind() to
 * a type that is one of the interfaces of the proxyed object, then call on the proxy instance,
 * 
 * <pre>
 * e.g:
 * IHello hello = (IHello) new TimerProxy().bind(new Hello());
 * hello.hello(&quot;eric&quot;);
 * </pre>
 * <p>
 * <b>way two</b>: static getProxy() is a simpler way to use, underling it use way one,
 * 
 * <pre>
 * e.g:
 * IHello hello = TimerProxy.getProxy(new Hello(), IHello.class);
 * hello.hello(&quot;eric&quot;);
 * </pre>
 * 
 * </p>
 * 
 * @author eric
 * @date Dec 7, 2012 4:14:27 PM
 */
public class TimerProxy implements InvocationHandler {
	private Object obj; // the object to be proxyed
	private static Logger logger = Logger.getLogger(TimerProxy.class);

	/**
	 * create a proxy instance
	 * 
	 * @param obj
	 *            the object to be proxyed
	 * @return the proxy instance
	 */
	public Object bind(Object obj) {
		this.obj = obj;
		return Proxy.newProxyInstance(this.obj.getClass().getClassLoader(), this.obj.getClass().getInterfaces(), this);
	}

	/**
	 * get a proxy instance
	 * 
	 * @param obj
	 * @return
	 */
	public static Object getProxy(Object obj) {
		return new TimerProxy().bind(obj);
	}

	/**
	 * get a proxy instance, and cast to specified type,
	 * 
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getProxy(Object obj, Class<T> clazz) {
		return (T) (getProxy(obj));
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object result = null;
		long start = System.currentTimeMillis();
		result = method.invoke(this.obj, args);
		logger.info(method.getDeclaringClass().getName() + "." + method.getName() + "() called, during " + (System.currentTimeMillis() - start)
				+ " milliseconds.");
		return result;
	}
}
 

 

ProxyTest.java

 

package eric.j2se.proxy;

/**
 * proxy test
 * 
 * @author eric
 * @date Dec 7, 2012 4:11:03 PM
 */
public class ProxyTest {
	public static void main(String[] args) {
		// test();
		test2();
	}

	public static void test() {
		IHello hello = (IHello) new TimerProxy().bind(new Hello());
		hello.hello("eric");
	}

	public static void test2() {
		IHello hello = TimerProxy.getProxy(new Hello(), IHello.class);
		hello.hello("eric");
	}
}
 

------


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值