The following is an example of the design pattern class:
1 import Java.lang.reflect.InvocationHandler;2 import Java.lang.reflect.Method;3 import Java.lang.reflect.Proxy;4 import java.util.List;5 import Java.util.Vector;6 7 8 Public classVectorproxy implements Invocationhandler {9 Ten PrivateObject targetobj; One A PublicObject GetObject () { - returntargetobj; - } the - Publicvectorproxy (Object Obj) { -Targetobj =OBJ; - } + - Public StaticObject Factorary (Object Obj) { + AClass CLA =Obj.getclass (); atObject proxy = proxy.newproxyinstance (Cla.getclassloader (), cla.getinterfaces (),New vectorproxy (OBJ)); - returnproxy; - } - - Publicobject Invoke (Object proxy, Method method, object[] args) - throws Throwable { inSystem. out. println ("befor invoke"+method.getname ()); - if(args!=NULL){ to for(Object O:args) { +System. out. println (O.tostring ()); - } the } * $Object tarobj =( (Vectorproxy) (Proxy.getinvocationhandler (Proxy))). GetObject ();Panax Notoginseng -System. out. println (tarobj==targetobj); the +Object resultobj =Method.invoke (Targetobj,args); A the returnresultobj; + } - $ Public Static voidMain (String args[]) { $List v =NULL; -v = (List) factorary (NewVector (Ten)); -V.add ("NEW"); theSystem. out. println (v.Get(0)); - }Wuyi the}
Results:
befor invoke AddNEWtruebefor invoke get0true NEW
I use this example to describe my understanding of dynamic agents.
Start with the main function entry. When I execute a v = (List) factorary (new Vector (10)), then the operation is 21~24, we look at 23 lines, the Proxy.newproxyinstance function generates an instance of the broker, but whose instance does it generate? Let's take a look at the prototype of this function: public static Object newproxyinstance (ClassLoader loader,class<?>[] interfaces, Invocationhandler h)
It generates an instance of interface Invocationhandler this interface, in this case the class Vectorproxy implements this interface and in 23 rows, the incoming class is passed in. This function returns object objects that implement all interfaces implemented by the proxy class, that is, dynamic proxies, which require a forced transformation . Here you can look at 47 lines, corresponding to this sentence (List) is a forced type conversion, class member variable targetobj is the proxy class, here is the vector, the implementation of all the interface is a vector of various operations such as Get,add these. in line 47, this vector object that implements all vector interfaces is assigned to V. The following is the interface Invocationhandler in this invoke function, this function is automatically called, 29~39 can be removed, useful is only 40 lines of this. Then as for the 40 line of this detail can go to my previous blog reflection mechanism of the further understanding to see, here no longer repeat.
Next, take a look at these corresponding outputs of the 29~39 line. The principle of these outputs after reading the reflection mechanism of the further understanding can be understood, I would like to say 37 lines of this sentence, first look at the proxy class member variables:
If you add a System.out.println to the factorary ("type of proxy" +proxy.getclass (). GetName ()); Then the corresponding output is the type of proxy $Proxy 0, in the definition of the class of the proxy we found that it has a proxyclassnameprefix, this prefix plus 0 is the proxy of our generated agent, so the 37 line of the Object Tarobj = ((Vectorproxy) (Proxy.getinvocationhandler (Proxy)). GetObject (); Here Proxy is a label, and then actually is that V is through this marking to carry out the add,get operation actually V (that is, the proxy, followed by V) and did not implement all the methods, It simply invokes these operations of V through the Java bottom call still handled by the function of the proxy object itself, and V is not referring to a new class that is not the same as the previous targetobj, it is just a high-level identifier that points to that class and can manipulate the class. Reasonable guess: The two map member variables in proxy should be used to store multiple proxy classes for the purpose of storing V (proxy). There's nothing more to say about it here.
Java Proxy Mode example explained