[Java] 13: proxy mode-dynamic proxy!

Source: Internet
Author: User

[Java] 13: proxy mode-dynamic proxy!

In the previous blog, we talked about a form of proxy mode-static proxy. At the end of this article, we mentioned the shortcomings of static proxy, one of them is that when there are many business classes, it is obviously a very bad choice to use static proxy. How can this problem be solved? This leads to our dynamic proxy.

What is dynamic proxy?

The so-called dynamic proxy is relative to the static proxy. The reflection mechanism is used to determine the class to be loaded during the runtime of the program, which effectively avoids the problem that a business class of static proxy corresponds to a proxy class.

Why dynamic proxy?

Dynamic proxy is used to solve the problem of poor scalability and difficult maintenance of static proxy. This is what I mentioned at the beginning of this article,

How to Use Dynamic proxy?

To use dynamic Proxy, you must use Proxy and InvocationHandler. Next I will use the example of the previous blog to show you how to use dynamic proxy.

The user-managed interfaces and implementations will not be described in detail. The following describes the dynamic proxy LogHandler and client code:

The dynamic proxy object class LogHandler that simulates the log function. The Code is as follows:

 

Public class LogHandler implements InvocationHandler {// target Object private Object targetObject;/*** instantiation method of this class, it is used to pass the target Object * @ param targetObject target Object * @ return */public Object newProxyInstance (Object targetObject) into this.tar getObject = targetObject; // call the static method newProxyInstance of the Proxy class to instantiate a Proxy class and pass the target object as a parameter to return Proxy. newProxyInstance (targetObject. getClass (). getClassLoader (), targetObject. getClass (). getInterfaces (), this);}/*** Method for executing the target Object */public Object invoke (Object proxy, method Method, Object [] args) throws Throwable {// print the System before the method of the target object is executed. out. println ("start execution method -->" + method. getName (); for (int I = 0; I

Client:

Public class Client {/*** @ param args */public static void main (String [] args) {// instantiate the dynamic proxy class LogHandlerLogHandler logHandler = new LogHandler (); // pass the user management implementation class into the dynamic proxy class UserManager userManager = (UserManager) logHandler. newProxyInstance (new UserManagerImpl (); // userManager. addUser ("0001", "Zhong Yuemin"); // userManager. delUser ("0001"); String name = userManager. findUser ("0001"); System. out. println ("Client. main () --- "+ name );}}

Summary:

The biggest advantage of dynamic proxy compared with static proxy is that all methods declared in the interface are transferred to the InvocationHandler. invoke method that calls the processor ). In this way, when there are a large number of interface methods, we can flexibly process them without the need to transfer each method like a static proxy. Moreover, the application of Dynamic proxy makes our class responsibilities more uniform and more reusable.

Whether it is static proxy or dynamic proxy, this idea of proxy is very instructive. Many frameworks and technologies have the shadows of proxies. For example, the essence of AOP in Spring is the proxy mode, and every framework in the SSH framework uses the proxy mode.

Related Article

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.