This blog corresponds to the previous blog "static proxy mode". Let's take a look at the dynamic proxy. The reason why static proxy is difficult to expand and maintain is that the Code is too dead to be replaced; what solutions can I think of when I write code? Yes, it is reflection.
Reflection can effectively solve the problem of determining which proxy class to load, avoiding repeated write for each proxy class.
Dynamic proxy
Interface usermanager
/***** User control interface * @ author administrator **/public interface usermanager {public void adduser (string userid, string username); Public void modifyuser (string userid, string username); Public void deluser (string userid); Public String finduser (string userid );}Implementation class usermanagerimpl
/***** Real implementation class of user management * @ author administrator **/public class usermanagerimpl implements usermanager {/******* Add User */Public void adduser (string userid, string username) {system. out. println ("Adding User..." + userid + username + "...... ");}/******* Delete user */Public void deluser (string userid) {system. out. println ("deluser, userid =" + userid);}/***** Search user */Public String finduser (string userid) {system. out. println ("finduser, userid =" + userid); Return userid;} public void modifyuser (string userid, string username) {system. out. println ("modifyuser, userid =" + userid );}}Agent loghandler
Import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; import Java. lang. reflect. proxy; public class loghandler implements invocationhandler {private object targetobject; public object newproxyinstance (Object targetobject) implements this.tar GetObject = targetobject; return proxy. newproxyinstance (targetobject. getclass (). getclassloader (), targetobject. getclass (). getinterfaces (), this);} public Object invoke (Object proxy, method, object [] ARGs) throws throwable {object ret = NULL; try {system. Out. println ("preparations before the operation ...... "); // Call the target method ret = method. Invoke (targetobject, argS); system. Out. println (" operation successful, confirmation processing ...... ");} Catch (exception e) {e. printstacktrace (); system. out. println ("error -->" + method. getname (); throw E;} return ret ;}}
Client
public class Client {/** * @param args */public static void main(String[] args) {LogHandler logHandler = new LogHandler();UserManager userManager = (UserManager)logHandler.newProxyInstance(new UserManagerImpl());userManager.findUser("0001");}}
Running result
Sequence Chart
Summary dynamic proxy mode through reflection, you can decide which class to load at runtime to avoid the problem that a class corresponds to a proxy. At the same time, through the unified invoke method, the process of the original function is unified by the proxy class, and the repeated code is greatly reduced by using dynamic proxy, which reduces the complexity and cost of maintenance.
For more DRP blogs, visit DRP project summary.