静态代理
注:实质就是使代理类和被代理类实现同一个接口,然后在代理类中添加其他操作。
MyProxy.java文件
package cn.qujialin.proxy; //代理类 public class MyProxy implements Operator{ private Operator operator; public MyProxy(Operator operator){ this.operator = operator; } @Override public int add(int i, int j) { System.out.println("proxy前"); operator.add(i,j); System.out.println("proxy后"); return 0; } @Override public int sub(int i, int j) { System.out.println("proxy前"); operator.sub(i,j); System.out.println("proxy后"); return 0; } @Override public int mul(int i, int j) { System.out.println("proxy前"); operator.mul(i,j); System.out.println("proxy后"); return 0; } @Override public int div(int i, int j) { System.out.println("proxy前"); operator.div(i,j); System.out.println("proxy后"); return 0; } } //被代理类 class MyOperator implements Operator{ @Override public int add(int i, int j){ System.out.println("add......"); return i + j; } @Override public int sub(int i, int j){ System.out.println("sub......"); return i - j; } @Override public int mul(int i, int j){ System.out.println("mul......"); return i * j; } @Override public int div(int i, int j){ System.out.println("div......"); return i / j; } }
Operato.java
package cn.qujialin.proxy; //接口 public interface Operator { public int add(int i, int j); public int sub(int i, int j); public int mul(int i, int j); public int div(int i, int j); }
TestMain.java
package cn.qujialin.proxy; //测试 public class TestMain { public static void main(String[] args) { Operator op = new MyProxy(new MyOperator()); op.add(1,2); } }
动态代理
MyActiveProxyFactory.java
package cn.qujialin.activeproxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; //代理工厂 public class MyActiveProxyFactory { public static Object getProxyInstance(Operator op){ Object proxy = null; ClassLoader cl = op.getClass().getClassLoader(); Class[] interfaces = op.getClass().getInterfaces(); InvocationHandler invocationHandler = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); System.out.println("methodName:::" + methodName); Object result = method.invoke(op, args); System.out.println("methodName:::" + methodName); return result; } }; return Proxy.newProxyInstance(cl,interfaces,invocationHandler); } }
Operator.java
package cn.qujialin.activeproxy; //接口 public interface Operator { public int add(int i, int j); public int sub(int i, int j); public int mul(int i, int j); public int div(int i, int j); }
MyOperator.java
package cn.qujialin.activeproxy; import cn.qujialin.activeproxy.Operator; //被代理类 public class MyOperator implements Operator{ @Override public int add(int i, int j){ System.out.println("add......"); return i + j; } @Override public int sub(int i, int j){ System.out.println("sub......"); return i - j; } @Override public int mul(int i, int j){ System.out.println("mul......"); return i * j; } @Override public int div(int i, int j){ System.out.println("div......"); return i / j; } }
TestMain.java
package cn.qujialin.activeproxy; //测试 public class TestMain { public static void main(String[] args) { Operator p = (Operator)MyActiveProxyFactory.getProxyInstance(new MyOperator()); p.add(3,5); } }