Agent Mode-Dynamic proxy

Source: Internet
Author: User

package pattern.proxy.dynamic;





import Java.lang.reflect.Proxy;


import Java.lang.reflect.InvocationHandler;


import Java.lang.reflect.Method;


/**


* Agent mode: Proxy pattern


* Java support for proxy mode:


* Java.lang.reflect.Proxy, Java.lang.reflect.InvocationHandler.


* @version 2009-6-27


* @author winty (wintys@gmail.com)


 */


public class dynamicproxytest{


public static void Main (string[] args) {


Subject Subject = new Realsubject ();


Subject proxy = (Subject) dynamicproxysubjectfactory.factory (Subject);


proxy.request ();


    }


}








/**


* Abstract Theme role


 */


Abstract interface subject{


public void request ();


}








/**


* Real Theme role


 */


class Realsubject implements subject{


@Override


public void Request () {


System.out.println ("do request.");


    }


}








class Dynamicproxyhandler implements invocationhandler{


private Subject Realsubject;





public Dynamicproxyhandler (Subject realsubject) {


this.realsubject = Realsubject;


    }





//proxy represents proxy object instance generated by proxy $proxy0


@Override


public Object Invoke (Object Proxy, Method method, object[] args)


throws throwable{


        


prerequest ();


Object result = Method.invoke (Realsubject, args);


postrequest ();





return result;


    }





private void Prerequest () {


System.out.println ("Do prerequest");


    }





private void Postrequest () {


System.out.println ("Do postrequest");


    }


}





/**


* Dynamic proxy class factory: Dynamically generate a proxy class by POxy class (e.g. $Proxy 0).


 */


class dynamicproxysubjectfactory{


    /**


* @param subject is the real object of the agent. Subject must be an interface,

The true type of
* i.e. subject is the class that implements the subject interface.


* Because the dynamic agent of JDK can not be arbitrary agent all,

The second parameter of the
* Proxy.newproxyinstance () determines only the proxy interface.


     *


* @return proxy class instance


     */


public static Object factory (Subject Subject) {


class<?> cls = Subject.getclass ();


Dynamicproxyhandler handler = new Dynamicproxyhandler (subject);





return Proxy.newproxyinstance (Cls.getclassloader (),


cls.getinterfaces (),


handler);


    }


}

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.