Talking about the dynamic agent

Source: Internet
Author: User
    /** * Matchmaking * * @author zhengt * @time June 3, 2095 3:13:03 PM/public interface  
    xiangqininterface {/** * Blind Date method * * public void xiangqin (); /** * John Blind Date Implementation class * * @author Zhengt * @time June 3, 2095 3:14:48 PM/public CLA SS Zhangsanxiangqininterfaceimpl implements Xiangqininterface {public void Xiangqin () {System.ou T.println ("John to Blind Date, marry a beautiful wife."  
        ");  
    }} import Java.lang.reflect.InvocationHandler;  
      
    Import Java.lang.reflect.Method; 
     /** * Blind Date is a lifetime of great events, before the blind date to prepare, dress up handsome. * * @author Zhengt * @time June 3, 2095 3:15:48 PM */public class Readyinvocationhandler Implemen  
      
        TS Invocationhandler {//Blind Date interface implementation class, that is, John Blind Date class private Object Zhangsan = null;  
   Public Readyinvocationhandler (Object realsubject) {This.zhangsan = Realsubject;     public object invoke (object proxy, Method M, object[] args {object result = NULL; 
                 The try {/** * Dynamic proxy class $proxy0 calls its own Xiangqin method when the Xiangqin method is invoked. * and its own Xiangqin method calls the Super.h.invoke (this,,), the invocation method of H of the parent proxy, which is the Readyinvocationhandler class in 
                 Voke method. * So, the proxy of Invoke (Object Proxy, Method M, object[] args) is actually a dynamic proxy class $proxy0, * if you turn it into xiangqininterface 
                 The Xiangqin method is called, and then it calls the Super.h.invoke (this,,), and then the loop is dead. * */** * Online about the most problem here is what object proxy is used to do here. I don't know about that, but at least we know what it is, what it is, and what it is. * * SYSTEM.OUT.PRINTLN (pro  
                Xy.getclass (). Getsimplename ()); System.out.println ("John before the Blind date, the agent dressed him up."  
                ");  
            result = M.invoke (Zhangsan, args); catch (Exception ex) {SYSTEM.EXIT (1);  
        return result;  
    }} import Java.lang.reflect.InvocationHandler;  
    Import Java.lang.reflect.Method;  
      
    Import Java.lang.reflect.Proxy; 
     /** * John came to the matchmaking agency (dating site), began blind date. * * @author Zhengt * @time June 3, 2095 3:17:16 PM */public class Hunjiesuo {public St atic void Main (String args[]) {//First instantiate the implementation class of the John Blind Date, that is to get an instance object of the Xiangqininterface interface Xiangqinin  
            Terface Zhangsan = new Zhangsanxiangqininterfaceimpl (); 
             /** * Gets a proxy class for the Zhangsanxiangqininterfaceimpl class, and binds a handler class Readyinvocationhandler for the proxy class. * Listening to a very round mouth, in fact, every time you call the Xiangqin method Zhangsanxiangqininterfaceimpl this subclass, * is not zhangsan this Zhangsanxiangqininterfaceimpl class instance to invoke, * but this Zhangsanxiangqininterfaceimpl proxy class Readyinvocationhandler to invoke its own Invoke method, * this Invok 
        In the E method, you can invoke the Xiangqin method of Zhangsan this instance. * */**     * How to implement a dynamic proxy in Java? * The first step, we need to have an interface, but also have an interface implementation class, and this implementation class is the object we want to proxy, * the so-called agent it is in the call to implement the class method, can be in the square 
             The act of doing additional work before and after the execution of the law is the agent. * In the second step, we're going to write ourselves a class that can do extra work when the method of the proxy class is executed, and the class must inherit the Invocationhandler interface, and why inherit it. 
             Because an instance of the proxy class does not tune this method of the real implementation class when invoking the method of implementing the class, it instead invokes the Invoke method of the class (the method that must be implemented when inheriting), in which you can invoke the method of the real implementation class. 
             * The third step, when you want to use an instance of the proxy class to invoke the implementation class method, write down the following two pieces of code. * * Xiangqininterface proxy = (xiangqininterface) proxy.newproxyinstance (zhangsan.getc Lass (). getClassLoader (), Zhangsan.getclass (). Getinterfaces (), New Readyinvocat  
            Ionhandler (Zhangsan));  
            Proxy.xiangqin (); 
             /** * Here to explain the meaning of the long section of code in the middle of the section, and what the specific work is. 
             * First, according to Zhangsan.getclass (). getClassLoader () the class loader and * Zhangsan.getclass () of the proxy class (). Getinterfaces () all interfaces implemented by the proxy class * Call Proxy.getproxyclass (ClassLoader loader, class<?> ... i) as a parameter.Nterfaces) * method returns the Java.lang.Class object of the proxy class, which is the class object of the proxy class $proxy0 generated by Java dynamically. 
             * At the same time, Java also let this dynamically generated $PROXY0 class implement all the interfaces to the implementation of the proxy class and inherit the proxy interface. * Second, instantiate an instance of this dynamically generated $proxy0 class, instantiate the constructor of the proxy class as proxy (Invocationhandler h), * that is to instantiate the dynamically generated $proxy0 class, you must give it a invocat 
             Ionhandler parameter, which is the class readyinvocationhandler that we implement to do extra work before and after the proxy class * method is implemented. * This Code proxy.newproxyinstance (Zhangsan.getclass (). getClassLoader (), Zhangsan.getclass (). Getinterfaces (), New 
             Readyinvocationhandler (Zhangsan)) * is actually a class called $proxy0 extends Proxy implements Xiangqininterface. 
             * Thirdly, the $proxy0 class is coerced into a xiangqininterface type, and the Xiangqin method is invoked.   */  
        }  
    }


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.