When reading Gigix's G-roller, a proxy model is used to achieve increased transaction processing for the business. In fact, many of the design patterns in the Java SDK are implemented directly, and observer is one of them. But because such APIs are rarely used, there is no chance to learn. And the example is the best teacher.
Package com.bonjovi.test;
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Proxy;
Import Java.lang.reflect.Method;
Import Java.util.Vector;
Import java.util.List;
public class Vectorproxy implements Invocationhandler {
Private Object proxyobj;
/** @link Dependency * *
* * #Proxy Lnkproxy; */
Public vectorproxy (Object obj) {
Proxyobj = obj;
}
public static object factory (object obj) {
Class cls = Obj.getclass ();
Return Proxy.newproxyinstance (Cls.getclassloader (),
Cls.getinterfaces (), New Vectorproxy (obj));
}
public object invoke (object proxy, Method method, object[] args)
Throws Throwable {
System.out.println ("before calling" + method);
if (args!= null) {
for (int i = 0; i < args.length; i++) {
System.out.println (Args[i] + "");
}
}
Object o = Method.invoke (proxyobj, args);
System.out.println ("After the Calling" + method);
return o;
}
public static void Main (string[] args) {
List v = null;
v = (List) factory (new Vector (10));
V.add ("New");
V.add ("York");
}
}