In this chapter, we will talk about proxy and dynamic proxy.
1. Why do I need an agent?
Answer: There are operations you don't want to integrate into your logical code, so you need an agent to do this.For example: Logging, monitoring, etc.
The following demonstrates a simple proxy:
package com.ray.ch11; public class Test { public static void test(InterFace interFace) { interFace.doSomething(); interFace.doElsething(); } public static void main(String[] args) { test(new RealObject()); test(new SimpleProxy(new RealObject())); } } interface InterFace { void doSomething(); void doElsething(); } class RealObject implements InterFace { @Override public void doSomething() { System.out.println("RealObject doSomething"); } @Override public void doElsething() { System.out.println("RealObject doElsething"); } } class SimpleProxy implements InterFace { private InterFace interFace; public SimpleProxy(InterFace interFace) { this.interFace = interFace; } @Override public void doSomething() { System.out.println("SimpleProxy doSomething"); interFace.doSomething(); } @Override public void doElsething() { System.out.println("SimpleProxy doElsething"); interFace.doElsething(); } }
Output:
RealObject doSomething
RealObject doElsething
SimpleProxy doSomething
RealObject doSomething
SimpleProxy doElsething
RealObject doElsething
As you can see from the code above, the basic requirement to implement a proxy is to implement only one interface between the real object and the proxy at the same time. In fact, the two sentences of output of our code above in the proxy are not integrated into the logical code, so we need the proxy class to handle these things.
2. Dynamic Proxy
java not only has the mode of proxy, but also creates proxy dynamically.
Let's look at the code below. We've replaced our static proxy with a dynamic proxy from java.
package com.ray.ch11; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class Test { public static void test(InterFace interFace) { interFace.doSomething(); interFace.doElsething(); } public static void main(String[] args) { InterFace proxy = (InterFace) Proxy.newProxyInstance(InterFace.class .getClassLoader(), new Class[] { InterFace.class }, new DynamicProxyHandler(new RealObject())); test(proxy); } } class DynamicProxyHandler implements InvocationHandler { private InterFace interFace; public DynamicProxyHandler(InterFace interFace) { this.interFace = interFace; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("proxy dosomething"); System.out.println(proxy.getClass()); System.out.println(method); return method.invoke(interFace, args); } } interface InterFace { void doSomething(); void doElsething(); } class RealObject implements InterFace { @Override public void doSomething() { System.out.println("RealObject doSomething"); } @Override public void doElsething() { System.out.println("RealObject doElsething"); } }
Output:
dosomething
class com.ray.ch11.$Proxy0
public abstract void com.ray.ch11.InterFace.doSomething()
RealObject doSomething
dosomething
class com.ray.ch11.$Proxy0
public abstract void com.ray.ch11.InterFace.doElsething()
RealObject doElsething
The code above does the same thing as the first point, but there are a few parameters to be aware of in Proxy.newProxyInstance
1. is a classLoader, this loader is generally a loader that has loaded objects. The loader above can be either an InterFace loader or a Test loader
2. List of interfaces you want to implement (here must be interfaces, not classes or abstract classes)
3. Implementation of InvocationHandler
4. In the implementation of DynamicProxyHandler, proxy is actually a proxy, not an actual class
5. Execute methods of real classes through method.invoke(interFace, args) method
6. We can even control the method of execution by the method name.
Summary: This chapter describes proxy and dynamic proxy.
Thank you for this chapter.
-----------------------------------
Reprinted at: https://my.oschina.net/u/2325575/blog/543729