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 ");
}
}