Java static proxy and dynamic proxy

Source: Internet
Author: User

I. Proxy

Call a class (delegate class, the class that actually completes the work) method to another class (proxy class, which can be statically or dynamically generated. If the delegate class and proxy class implement the same interface, the proxy can be conveniently completed.
Ii. Static proxy

Before running the program, the relationship between the proxy class and the delegate class has been determined. The bytecode file of the proxy class already exists and the proxy is not generated at runtime.

Code list 1, interface definition:

Package proxy. staticy;
Public interface Subject {
Void dealTask (String taskName );
}

Code list 2: Delegate class, the class that actually does things:

Package proxy. staticy;
Public class RealSubject implements Subject {
@ Override
Public void dealTask (String taskName ){
System. out. println ("executing task:" + taskName );
Try {
Thread. sleep (500 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}

Code list 3, proxy class:

Package proxy. staticy;
Public class ProxySubject implements Subject {
Private Subject delegate;
Public ProxySubject (Subject delegate ){
This. delegate = delegate;
}
@ Override
Public void dealTask (String taskName ){
Long start = System. currentTimeMillis ();
Delegate. dealTask (taskName );
Long end = System. currentTimeMillis ();
System. out. println ("task time consumption" + (end-start) + "millisecond ");
}
}

Code list 4: The factory class that generates the Proxy:

Package proxy. staticy;
Public class SubjectStaticFactory {
Public static Subject getInstance (){
Return new ProxySubject (new RealSubject ());
}
}

Code list 5, simulate the client:

Package proxy. staticy;
Public class Client1 {
Public static void main (String [] args ){
Subject myProxy = SubjectStaticFactory. getInstance ();
MyProxy. dealTask ("Task one ");
}
}

3. Dynamic proxy

The source code of the proxy class is generated by a virtual machine reflection at runtime, so there is no bytecode file. The relationship between the proxy class and the delegate class is determined at runtime.

Code list 6: The call processor corresponding to the dynamic Proxy: (To put it bluntly, it is similar to the proxy class)

/**
* Call processor corresponding to the dynamic proxy
*/
Package proxy. dynamic;
Import java. lang. reflect. InvocationHandler;
Import java. lang. reflect. Method;
Public class SubjectInvocationHandler implements InvocationHandler {

// The proxy class holds an object reference of the delegate class
Private Object delegate;

Public SubjectInvocationHandler (Object delegate ){
This. delegate = delegate;
}

@ Override
Public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {
Long start = System. currentTimeMillis ();

// Method invoke returns the result of executing the Object Method
Method. invoke (delegate, args );

Long end = System. currentTimeMillis ();

System. out. println ("task execution time" + (end-start) + "millisecond ");

Return null;
}
}

Code list 7: The factory class for generating dynamic proxy objects:

/**
* The factory that generates the dynamic proxy object.
*/
Package proxy. dynamic;
Import java. lang. reflect. InvocationHandler;
Import java. lang. reflect. Proxy;
Import proxy. staticy. RealSubject;
Import proxy. staticy. Subject;
/**
* The client calls this method to obtain the proxy object,
* The client does not know whether to obtain the proxy object or the delegate object.
*/
Public class DynamicProxyFactory {
Public static Subject getInstance (){
Subject delegate = new RealSubject ();
InvocationHandler handler = new SubjectInvocationHandler (delegate );
Subject proxy = (Subject) Proxy. newProxyInstance (
Delegate. getClass (). getClassLoader (),
New Class [] {Subject. class },
Handler );
Return proxy;
}
}

Code list 8, simulating the client:

Package proxy. dynamic;
Import proxy. staticy. Subject;
Public class Client2 {
Public static void main (String [] args ){
Subject myProxy = DynamicProxyFactory. getInstance ();
MyProxy. dealTask ("Task two ");
}
}

Related Article

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.