Design Mode-dynamic proxy (JDK) and design mode-dynamic jdk

Source: Internet
Author: User

Design Mode-dynamic proxy (JDK) and design mode-dynamic jdk

I wrote a small example in my previous blog about static proxy. At the end of the blog "design patterns-Agent patterns", James left a question. If James wants to add a test method for each method, he can test the efficiency of the agent. This blog will continue ......

The java. lang. reflect package provides a Proxy class and an InvocationHandler interface. By using this class and interface, you can generate JDK dynamic Proxy class or dynamic Proxy object.

Proxy provides a static method for creating dynamic Proxy classes and Proxy objects. It is also the parent class of all dynamic Proxy classes. If an implementation class is dynamically generated for one or more interfaces in the program, you can use Proxy to create a dynamic Proxy class. If you need to dynamically create an instance for one or more interfaces, you can also use Proxy to create a dynamic Proxy instance.

1. First, I wrote a special test time class.

Package dahuashejimoshiv3; import java. lang. reflect. invocationHandler; import java. lang. reflect. method;/*** @ author yingjie * call the Handler -- used to call the processing of the Method of the proxy Object */public class TestTime implements InvocationHandler {private Object target; // define the public TestTime (Object target) {this.tar get = target;}/*** proxy: represents the dynamic proxy Object * method: the method being executed * args: parameter */@ Overridepublic Object invoke (Object proxy, Method Method, Object [] args) // inherits the invoke method of the Proxy class. throws Throwable {long startMovieTicket = System. currentTimeMillis (); System. out. println ("Start Time:" + startMovieTicket); System. out. println (proxy. getClass (). getName () + "Ying Jie for testing, haha"); method. invoke (target); // execute the method based on the target parameter through reflection. This is the original method of the target object. Long endMovieTicket = System. currentTimeMillis (); System. out. println ("time used:" + (endMovieTicket-startMovieTicket); return null ;}}

2 client call:

Package dahuashejimoshiv1; import java. lang. reflect. invocationHandler; import java. lang. reflect. proxy; import dahuashejimoshi. v2.PursuitMethod; import dahuashejimoshi. v2.PursuitProxy; import into; public class Client {public static void main (String [] args) {SchoolGirl mmGirl = new SchoolGirl (); Pursuer xiaomingPursuiter = new Pursuer (mmGirl ); pursuitMethod pursuitProxy = new PursuitProxy (callback); // create an invocationHandler object InvocationHandler testUseTime = new TestTime (pursuitProxy); // use Proxy to directly generate a dynamic Proxy object PursuitMethod sMethod = (PursuitMethod) proxy. newProxyInstance (pursuitProxy. getClass (). getClassLoader (), pursuitProxy. getClass (). getInterfaces (), testUseTime); // call the generated dynamic proxy object method sMethod. sendFlowers (); sMethod. sendFootball (); sMethod. sendMovieTicket ();}}


3. Display Results:

Start Time: 1432891462497
Com. sun. proxy. $ Proxy0 Ying Jie, haha
James, the pursuer, gave me mmhua
Sending flowers via proxy
Time used: 0
Start Time: 1432891472415
Com. sun. proxy. $ Proxy0 Ying Jie, haha
James, the pursuer, gave the mm football
Proxy to help deliver the ball
Time used: 0
Start Time: 1432891472415
Com. sun. proxy. $ Proxy0 Ying Jie, haha
James, the pursuer, gave the mm movie ticket
The agent helps send movie tickets
Time used: 0


4. Question: How does JDK's Proxy and InvocationHandler implement dynamic Proxy?

Both static proxy and dynamic proxy cannot be separated from a proxy class. The difference is that static proxy can be created by ourselves. If we use JDK as our encapsulated dynamic proxy, it will automatically create a proxy class for US based on the conditions we provide. Simulate the jdk to write the generated dynamic proxy class proxy1, such as the leftmost class. The created dynamic proxy also implements the common interface pursuitmehtodd and overrides the methods in the interface. When the client calls a method in the dynamic proxy class, it calls the TestTime class inherited from InvocationHandle, and then calls the original method of the proxy object through reflection callback in the invoke method of TestTime.



5 What are the benefits of using dynamic proxy?

(1) Add more operations to the class without changing the original class. It is a non-intrusive code extension.


6. Conclusion:

After reading the information about dynamic proxy, I think it is not that difficult. Reflection is the key aspect of JDK-encapsulated dynamic Proxy. Method reflection, which is not studied yet and will be introduced in future articles.



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.