Dynamic proxy Mode

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.