Dynamic proxy (Dynamic proxy Mode)

Source: Internet
Author: User

Dynamic proxy (runtime behavior) mainly includes a proxy class and an invocationhandler interface.

Dynamic proxy role:

1. Abstract topic role

2. Real topic role (implemented the abstract topic Interface)

3. Dynamic proxy topic role (implements the invocationhandler interface and the invoke () method)

Proxy needs to call the newproxyinstance Method

 

Code Demonstration:

 

1. Abstract topic role subjectdemo. Java

 

Package com. dynamicproxy; </P> <p> Public interface subjectdemo <br/>{< br/> Public void request (); <br/>}< br/>

 

2. Real topic role realsubjectdemo. Java

 

Package COM. dynamicproxy; </P> <p> public class realsubjectdemo implements subjectdemo <br/>{< br/> Public void request () <br/>{< br/> system. out. println ("implemented a request"); <br/>}< br/>}

 

3. Dynamic proxy topic role dynamicproxysubjectdemo. Java

 

Package COM. dynamicproxy; </P> <p> Import Java. lang. reflect. invocationhandler; <br/> Import Java. lang. reflect. method; </P> <p> public class dynamicproxysubjectdemo implements invocationhandler <br/>{< br/> private object sub; </P> <p> Public dynamicproxysubjectdemo (Object OBJ) <br/>{< br/> This. sub = OBJ; <br/>}</P> <p> Public object invoke (Object proxy, method, object [] ARGs) throws throwable <br/>{< br/> system. out. println ("before" + method); </P> <p> method. invoke (sub, argS); // real call method operation </P> <p> system. out. println ("after" + method); </P> <p> return NULL; </P> <p >}</P> <p>}

4. Client. Java

 

Package COM. dynamicproxy; </P> <p> Import Java. lang. reflect. invocationhandler; <br/> Import Java. lang. reflect. proxy; </P> <p> public class client <br/> {<br/> Public static void main (string [] ARGs) <br/>{< br/> realsubjectdemo RSD = new realsubjectdemo (); </P> <p> invocationhandler handler = new dynamicproxysubjectdemo (RSD ); </P> <p> class <?> Classtype = handler. getclass (); </P> <p> // classtype. getclassloader (): Class Loader of the dynamic proxy class <br/> // RSD. getclass (). list of interfaces to be implemented by the getinterfaces () proxy class <br/> // call handler for Handler to assign a method call <br/> subjectdemo SD = (subjectdemo) proxy. newproxyinstance (classtype. getclassloader (), RSD. getclass (). getinterfaces (), Handler); </P> <p> // This line of code is executed and transferred to invocationhandler handler = new dynamicproxysubjectdemo (RSD) <br/> // execute the invoke method <br/> SD. request (); <br/>}< br/>

 

 

 

 

 


 

 

 

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.