一:静态代理模式
package staticProxy;
interface Star {
/**
* 面谈
*/
void confer();
/**
* 签合同
*/
void signContract();
/**
* 订票
*/
void bookTicket();
/**
* 唱歌
*/
void sing();
/**
* 收钱
*/
void collectMoney();
}
class RealStar implements Star {
@Override
public void bookTicket() {
System.out.println("RealStar.bookTicket()");
}
@Override
public void collectMoney() {
System.out.println("RealStar.collectMoney()");
}
@Override
public void confer() {
System.out.println("RealStar.confer()");
}
@Override
public void signContract() {
System.out.println("RealStar.signContract()");
}
@Override
public void sing() {
System.out.println("RealStar(周杰伦本人).sing()");
}
}
class ProxyStar implements Star {
private Star star;
public ProxyStar(Star star) {
super();
this.star = star;
}
@Override
public void bookTicket() {
System.out.println("ProxyStar.bookTicket()");
}
@Override
public void collectMoney() {
System.out.println("ProxyStar.collectMoney()");
}
@Override
public void confer() {
System.out.println("ProxyStar.confer()");
}
@Override
public void signContract() {
System.out.println("ProxyStar.signContract()");
}
@Override
public void sing() {
star.sing();
}
}
public class StaticProxy {
public static void main(String[] args) {
// TODO Auto-generated method stub
Star real = new RealStar();
Star proxy = new ProxyStar(real);
proxy.confer();
proxy.signContract();
proxy.bookTicket();
proxy.sing();
proxy.collectMoney();
}
}
二:Jdk动态代理模式
该动态代理需要提供接口
1:被代理类的接口
package dynamicproxy;
public interface HelloWorld {
public void sayHelloWorld();
}
2:被代理类
package dynamicproxy;
public class HelloWorldImp1 implements HelloWorld {
@Override
public void sayHelloWorld() {
// TODO Auto-generated method stub
System.out.println("hello world");;
}
}
3:依赖Jdk类库生成代理类的类
package dynamicproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JdkProxyExample implements InvocationHandler{
//真实对象
private Object target=null;
/**
* @return代理对象
*/
public Object bind(Object target)
{
this.target=target;
return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(), this);
}
/**
* 代理方法逻辑,代理类调用被代理方法时,被自动调用
* @param proxy 代理对象
* @param methon 当前调度方法
* @param args 当前方法参数
* @return 代理结果返回
* @throws Throwable 异常
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
System.out.println("进入代理逻辑方法");
System.out.println("在调度真实对象之前的服务");
Object obj=method.invoke(target, args);
System.out.println("在调度真实对象之后的服务");
return obj;
}
}
4:动态代理测试类JdkProxyTest
package dynamicproxy;
public class JdkProxyTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
JdkProxyExample jdk=new JdkProxyExample();
HelloWorld proxy=(HelloWorld)jdk.bind(new HelloWorldImp1());
proxy.sayHelloWorld();
}
}
三:拦截器
由于动态代理一般比较难理解,程序设计者会设计一个拦截器供开发者使用。现在用Jdk动态代理实现一个拦截器的逻辑。在上面Jdk动态代理的基础上增加以下代码。
1:拦截器接口
package dynamicproxy;
import java.lang.reflect.Method;
public interface Interceptor {
/**
* before返回true,直接调用真实对象方法,返回false调用around方法。在最后执行after方法
* @param proxy
* @param target
* @param method
* @param args
* @return
*/
public boolean before(Object proxy,Object target,Method method,Object[] args);
public void around(Object proxy,Object target,Method method,Object[] args);
public void after(Object proxy,Object target,Method method,Object[] args);
}
2:拦截器实现类
package dynamicproxy;
import java.lang.reflect.Method;
public class MyInterceptor implements Interceptor{
@Override
public boolean before(Object proxy, Object target, Method method, Object[] args) {
// TODO Auto-generated method stub
System.out.println("反射方法前逻辑");
return false;
}
@Override
public void around(Object proxy, Object target, Method method, Object[] args) {
// TODO Auto-generated method stub
System.out.println("取代了被代理对象的方法");
}
@Override
public void after(Object proxy, Object target, Method method, Object[] args) {
// TODO Auto-generated method stub
System.out.println("反射方法后逻辑");
}
}
3:生成带有拦截器的代理类的类
package dynamicproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class InterceptorJdkProxy implements InvocationHandler
{
private String interceptorClass=null;
private Object target;
public static Object bind(Object target,String interceptorClass)
{
return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(), new InterceptorJdkProxy(interceptorClass, target));
}
public InterceptorJdkProxy(String interceptorClass, Object target) {
super();
this.interceptorClass = interceptorClass;
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
if(interceptorClass==null)
{
return method.invoke(target, args);
}
Object result=null;
//通过反射生成拦截器
Interceptor interceptor=(Interceptor)Class.forName(interceptorClass).newInstance();
if(interceptor.before(proxy, args, method, args))
{
result=method.invoke(target, args);
}
else {
interceptor.around(proxy, args, method, args);
}
interceptor.after(proxy, args, method, args);
return result;
}
}
4:测试类
package dynamicproxy;
public class MyInterceptorTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloWorld proxy=(HelloWorld)InterceptorJdkProxy.bind(new HelloWorldImp1(),
"dynamicproxy.MyInterceptor");
proxy.sayHelloWorld();
}
}