Dynamic Proxy-Dynamic Proxy in Design Mode

Source: Internet
Author: User

 

Dynamic Proxy)JDK5 is a new feature. The feature is that the agent class and its objects are dynamically created at the runtime of the program, unlike the proxy class that must be defined before compilation when we use static proxy. At runtime, the framework helps us dynamically create a proxy class that implements multiple interfaces. Each proxy class object is associated with the Implementation class of an InvocationHandler interface. When we call the method in the interface proxy object, the call information will be passed to the InvocationHandler's invoke method. In the parameters of the invoke Method, you can obtain the proxy object, the Method object corresponding to the Method, and the actual parameters of the Call (internally implemented through reflection ). The Return Value of the invoke method is returned to the user. You can define the returned value by yourself. This is equivalent to Implementing AOP interception on method calls.

  To create a dynamic proxy, follow these steps:1. Create a class that implements the InvocationHandler interface. It must implement the invoke method. 2. Create the Proxy Class and Interface 3. Use the static Proxy method newProxyInstance (ClassLoader loader, Class <?> [] Interfaces, InvocationHandler h) Create a proxy 4. Call the method through the proxy (this proxy implements the interface of the proxy class)
/*** DynamicSubject. java * dynamicproxy ** Function: Dynamic proxy class, java. lang. reflect. proxy is the Class generated at runtime. When generating it, you must pass * a group of Interfaces to it (multiple Interfaces), and then the returned objects implement these Interfaces, this Proxy is a pure Porxy, * so we must provide an InvocationHandler, it is used to replace ** ver date author * ── ─ * 2011-6-12 Leon ** Copyright (c) 2011, TNT All Rights Reserved. */package dynamicproxy; import java. lang. reflect. invocationHandler; imp Ort java. lang. reflect. method; import java. lang. reflect. proxy;/*** ClassName: DynamicSubject * Function: todo add function * follow these steps to create a dynamic Proxy: * 1. create a class that implements the InvocationHandler interface. It must implement the invoke method. * 2. create the proxy class and interface * 3. use the static Proxy method newProxyInstance (ClassLoader loader, * Class [] interfaces, InvocationHandler h) to create a Proxy * 4. call the method through a proxy (this proxy implements the interface of the proxy class) *** Reason: todo add reason ** @ author Leon * @ version * @ since Ver 1.1 * @ Date 2011-6-12 */public class DynamicProxy implements InvocationHandler {private Object sub; public Object getSub () {return sub;} public void setSub (Object sub) {this. sub = Sub;} public DynamicProxy (Object obj) {this. sub = obj;} // proxy is the proxy class dynamically generated by the Framework @ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {// TODO Auto-generated method stubObject result = null; System. out. println ("before calling:" + method); // The result = method is the real proxy object. invoke (sub, args); System. out. println ("after calling... "); return result;} public static void main (String... Args) {RealSubject realSubject = new RealSubject (); DynamicProxy dynamicProxy = new DynamicProxy (realSubject); // InvocationHandler invocationHandler = dynamicProxy; // dynamically generate a proxy object, the returned object is neither a RealSubject instance nor a DynamicPorxy instance, but a dynamically generated instance $ prxoy0 which implements realSubject. getClass (). getInterfaces () These interfaces are ISubject subject = (ISubject) Proxy. newProxyInstance (dynamicProxy. getClass (). getClassLoader (), realSubject. getcia Ss (). getInterfaces (), dynamicProxy); // when this sentence is called, $ proxy0 calls the invocationHandler method passed in above, so the above method must be passed in invocationHandler. System. out. println ("-------------------------- Porxy realSubject1 --------------------------------------"); subject. request (); String result = subject. test (); System. out. println ("return by test method:" + result); System. out. println ("-------------------------- Proxy realSubject2 ----------------------------------------"); dynamicProxy. setSub (new RealSubject2 (); subject. request (); System. out. println ("proxy generate by system is:" + subject. getClass () ;}} interface ISubject {public void request (); public String test ();} class RealSubject implements ISubject {@ Overridepublic void request () {// TODO Auto-generated method stubSystem. out. println ("From real subject... request method .... ") ;}@ Overridepublic String test () {// TODO Auto-generated method stubString str =" From real subject ..... test method ..... "; return str ;}} class RealSubject2 implements ISubject {@ Overridepublic void request () {// TODO Auto-generated method stubSystem. out. println ("From real subject2... request method .... ") ;}@ Overridepublic String test () {// TODO Auto-generated method stubSystem. out. println ("From real subject2 ..... test method ..... "); return null ;}}

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.