-------------------- Android training, Java training, and hope to communicate with you! ----------------------
The Java proxy is another kind of knowledge that I think I have to post after learning it.
First of all, let's talk about what a proxy is, and quote Mr Zhang's statement, like a Lenovo computer agent store, where the Lenovo computer headquarters is located in Beijing (I don't know if it is true, as Mr. Zhang said ), I am in Guangzhou, and I want to buy a Lenovo computer. At this time, I have two options: one is to buy a car in Beijing, and the other is to buy a Lenovo agent in Guangzhou, obviously, many people choose the second method, which is why proxy is required, why proxy is required, and what role proxy is used.
The proxy mode provides a proxy for other objects to control access to this object. In some cases, a customer does not want to or cannot directly reference another object, the proxy object can provide an intermediary between the customer and the target object.
Proxy can be divided into two types: static proxy and dynamic proxy.
Static proxy is generally used to inherit a class that requires proxy, then rewrite its method, and make slight modifications on this basis to achieve what we want, but this method is generally very troublesome, if there are many methods for this class, it is very troublesome to implement it. So it is rarely used. The following describes dynamic proxy.
When it comes to dynamic proxy, you have to mention an interface invacationhandler. To implement dynamic proxy, you must implement the invocationhandler interface, which defines only one method.PublicObject invoke (Object OBJ, methodmethod, object [] objs), where OBJ refers to the proxy class, and the second parameter method refers to the method to be proxies, the third parameter objs refers to the parameter array of the proxy method. By implementing the interface to override the invoke method, we can modify the proxy method and add some attributes, for example, if we want to write an arraylist proxy and add the display method execution time to their methods (or rewrite the example provided by instructor Zhang), we can define a proxy class:
Class arraylistproxy {private object OBJ; public object BIND (Object OBJ) {This. OBJ = OBJ; return proxy. newproxyinstance (obj. getclass (). getclassloader (), obj. getclass (). getinterfaces (), new invocationhandler () {public object invoke (Object proxy, method, object [] ARGs) throws throwable {long begintime = system. currenttimemillis (); object ral = method. invoke (arraylistproxy. this. OBJ, argS); long endtime = system. currenttimemillis (); system. out. println (method. getname () + "() the runtime is" + (endtime-begintime); Return ral ;}});}}
The method in the class provides a parameter, the input parameter is the class to be proxy, the arraylistproxy class can return the reference of this class proxy (this class is too awesome), and then the test
Public static void main (string [] ARGs) {arraylist Al = new arraylist (); arraylistproxy phosphatase = new arraylistproxy (); Collection proxy = (Collection) phosphatase. BIND (Al); proxy. add ("aaaaaa"); proxy. add ("bbbbbb"); proxy. add ("ccccccc"); system. out. println (proxy. size ());}
However, in the above example, you may have a major problem with dynamic proxy, that is, the implemented proxy class is actually only the interface proxy. In the above example, it is the proxy of the collection interface, so is there any other method? I am very ashamed. I didn't expect that at most I can only implement the proxy of the interface implemented by arraylist one by one, which is still a little troublesome. I don't know if there are other methods. Unfortunately, I haven't found them yet. It's depressing.
-------------------- Android training, Java training, and hope to communicate with you! ----------------------
For details, see edu.csdn.net/heima.