package 代理模式;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKMeipo implements InvocationHandler{
// 被代理的对象,把引用保存下来
private Object target;
public Object getInstance(Object target) throws Exception{
this.target = target;
Class<?> clazz = target.getClass();
return Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),this);
}
public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable{
before();
Object obj = method.invoke(this.target,args);
after();
return obj;
}
private void before(){
System.out.println("我是媒婆:我给你找对象,现在请发送你的个人信息");
System.out.println("开始确认---------");
}
private void after(){
System.out.println("对不起,不能早恋!");
System.out.println("终止业务-------");
}
}