一、Static Proxy
interface HelloService {
public void hello(String msg);
}
class HelloServiceImpl implements HelloService {
public void hello(String msg) {
System.out.println("hello, " + msg);
}
}
class HelloServiceProxy implements HelloService {
private HelloService helloService;
public HelloServiceProxy(HelloService helloService) {
this.helloService = helloService;
}
public void hello(String msg) {
helloService.hello(msg);
}
}
public class StaticProxyExample {
public static void main(String[] args) {
HelloService helloService = new HelloServiceImpl();
HelloService helloServiceProxy = new HelloServiceProxy(helloService);
helloServiceProxy.hello("world!");
}
}
二、Dynamic Proxy
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
class MyInvocationHandler implements InvocationHandler {
private Object target;
private MyInvocationHandler(Object target) {
this.target = target;
}
public static Object createProxy(Object obj) {
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new MyInvocationHandler(obj));
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(target, args);
return result;
}
}
public class DynamicProxyExample {
public static void main(String[] args) {
HelloService helloService = new HelloServiceImpl();
HelloService helloServiceProxy = (HelloService) MyInvocationHandler.createProxy(helloService);
helloServiceProxy.hello("world!");
}
}
三、TracingProxy
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface Dog {
void bark();
}
class DogImpl implements Dog {
public void bark() {
}
}
class TracingIH implements InvocationHandler {
private Object target;
private TracingIH(Object obj) {
target = obj;
}
public static Object createProxy(Object obj) {
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new TracingIH(obj));
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
try {
System.out.println(method.getName() + "() called");
result = method.invoke(target, args);
} catch (InvocationTargetException e) {
System.out.println(method.getName() + " throws " + e.getCause());
throw e.getCause();
}
System.out.println(method.getName() + "() returns");
return result;
}
}
class DogFactory {
private Class<?> dogClass;
private boolean traceIsOn = false;
public DogFactory(String className, boolean trace) {
try {
dogClass = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e); // or whatever is appropriate
}
traceIsOn = trace;
}
public Dog newInstance() {
try {
Dog d = (Dog) dogClass.newInstance();
if (traceIsOn) {
d = (Dog) TracingIH.createProxy(d);
}
return d;
} catch (InstantiationException e) {
throw new RuntimeException(e); // or whatever is appropriate
} catch (IllegalAccessException e) {
throw new RuntimeException(e); // or whatever is appropriate
}
}
}
public class TracingProxyExample {
public static void main(String[] args) {
DogFactory factory = new DogFactory("DogImpl", true);
Dog dog = (Dog) factory.newInstance();
dog.bark();
}
}