public interface Subject {
void request();
}
// 请求的真实目标对象
public class RealSubject implements Subject {
@Override
public void request() {
System.out.println("real subject execute request");
}
}
// 代理对象
public class Proxy implements Subject {
private RealSubject realSubject;
public Proxy(RealSubject realSubject) {
this.realSubject = realSubject;
}
@Override
public void request() {
System.out.println("before");
try {
realSubject.request();
} catch (Exception e) {
System.out.println("ex:" + e);
throw e;
} finally {
System.out.println("after");
}
}
}
public class Client {
public static void main(String[] args) throws Exception {
Subject subject = new Proxy(new RealSubject());
subject.request();
}
}
before
real subject execute request
after
public class JdkProxySubject implements InvocationHandler {
private RealSubject realSubject;
public JdkProxySubject(RealSubject realSubject) {
this.realSubject = realSubject;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before");
Object result = null;
try {
result = method.invoke(realSubject, args);
} catch (Exception e) {
System.out.println("ex" + e);
throw e;
} finally {
System.out.println("after");
}
return result;
}
}
public class Client {
public static void main(String[] args) throws Exception {
Subject subject = (Subject) Proxy.newProxyInstance(Client.class
.getClassLoader(), new Class[]{Subject.class}, new JdkProxySubject(new RealSubject()));
subject.request();
}
}
before
real subject execute request
after
public interface Subject {
void request();
void hello();
}
public class RealSubject implements Subject {
@Override
public void request() {
System.out.println("real subject execute request");
}
@Override
public void hello() {
System.out.println("real subject execute hello");
}
}
public class Proxy implements Subject {
private RealSubject realSubject;
public Proxy(RealSubject realSubject) {
this.realSubject = realSubject;
}
@Override
public void hello() {
...
}
}
public class Client {
public static void main(String[] args) throws Exception {
Subject subject = (Subject) Proxy.newProxyInstance(Client.class
.getClassLoader(), new Class[]{Subject.class}, new JdkProxySubject(new RealSubject()));
subject.hello();
}
}
before
real subject execute hello
after
public class DemoMethodInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object object, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("before in cglib");
Object result = null;
try {
result = proxy.invokeSuper(object,args);
}catch (Exception e){
System.out.println("get ex :" + e.getMessage());
throw e;
}finally {
System.out.println("after in cglib");
}
return result;
}
}
public class Client {
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(RealSubject.class);
enhancer.setCallback(new DemoMethodInterceptor());
Subject subject = (Subject) enhancer.create();
subject.request();
}
}
before in cglib
real subject execute request
after in cglib
代理模式实例.png (20.89 KB, 下载次数: 9)
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |