The application of dynamic proxy reflection

The use of dynamic proxy, reflection is the key to dynamic languages

TestDynamicProxy

Package com.aff.dynamic; 

Import java.lang.reflect.InvocationHandler;
 Import the java.lang.reflect.Method;
 Import the java.lang.reflect.Proxy; 

/ * 
 dynamic proxy client refers to a method to invoke other objects via the proxy class , 
 and is dynamically created proxy object target class at runtime as needed. 
 Dynamic Agent Use: 
                            Debugging 
                            Remote Method Invocation 
principle proxy design pattern: 
                   use a proxy to wrap up an object, and then replace the original object with the proxy object. 
                   Any call to go through the original object proxy proxy object decide whether and when. the method calls to the original object. 
 * / 


// dynamic proxies, reflecting the key dynamic language 
interface the Subject {
     void Action (); 
} 

// the proxy class 
class RealSubjectthe implements Subject {
     public  void Action () { 
        System.out.println ( "I was the proxy class, remember to do" ); 
    } 
} 

// proxy class 
class MyInvocationHandler the implements InvocationHandler { 
    Object obj; // implements the proxy interfaces object class declaration
     //                  ① proxy class to the object is instantiated
     //                  ② returns proxy object class 

    public object Blind (object obj) {
         the this .OBJ = obj;
         // the proxy: a proxy class
         // the Proxy.newProxyInstance : examples of a new class of agents, but also to achieve the corresponding interfaces are implemented in the proxy class abstract methods
         //. obj.getClass () getClassLoader (): obj and the class loader is the same, and the same is the obj proxy object of the class class loader
         // . obj.getClass () The getInterfaces (); proxy class is implemented What interface proxy class (proxy), too
         // the this: to achieve a class object InvocationHandler interface, which is MyInvocationHandler
         // this step a real return target of a proxy class 
        return Proxy.newProxyInstance (obj.getClass () getClassLoader (. ), obj.getClass () the getInterfaces (),. the this ); 
    } 

    // whenever an object proxy class calls the action method to be overridden, it will be converted to the invoke method call as 
    @Override
     public Object invoke ( proxy Object, method, method, Object [] args)   throws the Throwable {
         // returnVal return value corresponding to the proxy class is an abstract method (action) return value
        Method.invoke returnVal = Object (obj, args); // obj is the proxy class 
        return returnVal; 
    } 
} 

public  class TestDynamicProxy {
     public  static  void main (String [] args) {
         // 1. the proxy object of the class 
        RealSubject real = new new RealSubject ();
         // 2. create a realization InvocationHandler interface class object 
        MyInvocationHandler Handler = new new MyInvocationHandler ();
         // 3. invoke blind () method returns an equally dynamic real class implements the interface where the implementation of proxy object 
         Object obj = handler.blind (Real); // Real type is subject, Object received by
         Sub = Subject (Subject) obj; // get strong turn it over and then call the action, sub proxy class is the object of this time, 
         
         sub.action (); // Go to InvocationHandler implementation of the interface class (MyInvocationHandler) of invoke ( ) method call 
    } 
}

 

Guess you like

Origin www.cnblogs.com/afangfang/p/12625808.html