Through the analysis of the Java Reflection mechanism (i) and the analysis of the Java Reflection Mechanism (ii) of the study, has a certain understanding of reflection, this article through the dynamic Proxy example to further learn the reflection mechanism.
1. Proxy mode
The proxy mode is to provide a proxy for other objects to control access to the object. In fact, the proxy mode is to introduce some degree of indirection when accessing the object, this kind of indirection can attach a variety of purposes.
It is characterized in that the proxy class and the delegate class have the same interface, the proxy class is mainly responsible for the delegate class preprocessing messages, filtering messages, forwarding messages to the delegate class, and post-processing messages. There is usually an association between the proxy class and the delegate class, and the object of a proxy class is associated with an object of a delegate class, and the object of the proxy class does not actually implement the service, but instead provides a specific service by invoking the related method of the object of the delegate class.
2. Classification
The proxy class can be divided into two kinds, static proxy class and dynamic proxy class according to the creation period.
Static proxy classes: Created by a programmer or generated automatically by a particular tool, and then compiled by the source code. Before the program runs, the. class file for the proxy classes already exists.
Dynamic proxy classes: When the program is running, it is created dynamically using the reflection mechanism.
3. Static Proxy and dynamic proxy examples
Static Proxy:
Business Interface class:
Package Com.bjpowernode.pattern; Public Interface Usermanager { publicvoid addUser (String userId, String userName); Public void Deluser (String userId); Public void ModifyUser (String userId, String userName); Public string Finduser (string userId);
Business interface Implementation class:
PackageCom.bjpowernode.pattern; Public classUsermanagerimplImplementsUsermanager { Public voidAddUser (String userId, String userName) {//System.out.println ("Start-->>adduser () userid-->>" + userId); Try{System.out.println ("Usermanagerimpl.adduser () userid-->>" +userId); //System.out.println ("Success-->>adduser ()"); }Catch(Exception e) {e.printstacktrace (); //System.out.println ("Error-->>adduser ()"); Throw Newruntimeexception (); } } Public voidDeluser (String userId) {System.out.println ("Usermanagerimpl.deluser () userid-->>" +userId); } Publicstring Finduser (String userId) {System.out.println ("Usermanagerimpl.finduser () userid-->>" +userId); return"Zhang San"; } Public voidModifyUser (String userId, String userName) {System.out.println ("Usermanagerimpl.modifyuser () userid-->>" +userId); } }
Business proxy class:
PackageCom.bjpowernode.pattern; Public classUsermanagerimplproxyImplementsUsermanager {PrivateUsermanager Usermanager; Publicusermanagerimplproxy (Usermanager usermanager) { This. Usermanager =Usermanager; } Public voidAddUser (String userId, String userName) {Try{System.out.println ("Start-->>adduser () userid-->>" +userId); Usermanager.adduser (UserId, userName); System.out.println ("Success-->>adduser ()"); }Catch(Exception e) {e.printstacktrace (); System.out.println ("Error-->>adduser ()"); } } Public voidDeluser (String userId) {} Publicstring Finduser (String userId) {return NULL; } Public voidModifyUser (String userId, String userName) {}}
Client class:
PackageCom.bjpowernode.pattern; Public classClient {/** * @paramargs*/ Public Static voidMain (string[] args) {//Usermanager Usermanager = new Usermanagerimpl (); Usermanager Usermanager =NewUsermanagerimplproxy (NewUsermanagerimpl ()); Usermanager.adduser ("0001", "Zhang San"); } }
Operation Result:
Start-->>adduser () userid-->>0001 usermanagerimpl.adduser () userId-->>0001 Success-->>adduser ()
Dynamic Agent:
Business Interface class:
Package Com.bjpowernode.pattern; Public Interface Usermanager { public string Test (string userId); }
Business interface Implementation class:
Package Com.bjpowernode.pattern; Public class Implements Usermanager { public string Test (String userId) { System.out.println (" Usermanagerimpl.finduser () userid-->> "+ userId"; return "Zhang San"; } }
Businesshandler class:
PackageCom.bjpowernode.pattern; ImportJava.lang.reflect.InvocationHandler; ImportJava.lang.reflect.Method; ImportJava.lang.reflect.Proxy; Public classBusinesshandlerImplementsInvocationhandler {PrivateObject TargetObject; PublicObject Newproxyinstance (Object targetObject) { This. TargetObject =TargetObject; returnproxy.newproxyinstance (Targetobject.getclass (). getClassLoader (), Targetobject.get Class (). Getinterfaces (), This); } Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("Start-->>" +method.getname ()); for(inti=0; i<args.length; i++) {System.out.println (args[i]); } Object ret=NULL; Try { //calling the target methodRET =Method.invoke (TargetObject, args); System.out.println ("Success-->>" +method.getname ()); }Catch(Exception e) {e.printstacktrace (); System.out.println ("Error-->>" +method.getname ()); Throwe; } returnret; } }
Client class:
PackageCom.bjpowernode.pattern; ImportJava.lang.reflect.Field; Public classClient {/** * @paramargs*/ Public Static voidMain (string[] args) {Businesshandler Businesshandler=NewBusinesshandler (); Usermanager Usermanager= (Usermanager) businesshandler.newproxyinstance (NewUsermanagerimpl ()); //usermanager.adduser ("0001", "Zhang San"); //usermanager.deluser ("0001"); //System.out.println (Usermanager.getclass (). GetName ()); String name= Usermanager.test ("0001"); //String name = ((Usermanagerimpl) loghandler.newproxyinstance (New Usermanagerimpl ())). Test ("0001"); System.out.println ("Client.main ()---" +name); } }
Results
Start-->>test 0001 usermanagerimpl.finduser () userid-->>0001 success-->>test Client.main ()---Zhang San
Analysis of the reflection mechanism of Java (III.)-A brief talk on dynamic agent