package com.wangyu01;
public interface Subject {
public void request();
}
package com.wangyu01;
public class RealSubject implements Subject {
public void request() {
System.out.println("真实实现--》关注做菜");
}
}
package com.wangyu01;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyHandle implements InvocationHandler {
private Object obj = null;
public Object bind(Object obj) {
this.obj = obj;
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
.getClass().getInterfaces(), this);
}
public Object invoke(Object arg0, Method method, Object[] arg2)
throws Throwable {
System.out.println("代理开始之前");
Object o =method.invoke(this.obj, arg2);
System.out.println("代理开始之后");
return o;
}
}
package com.wangyu01;
public class TestProxy {
public static void main(String[] args) {
ProxyHandle ph = new ProxyHandle();
Subject sub = (Subject)ph.bind(new RealSubject());
sub.request();
}
}