????------------------------------ ??????(Proxy) -------------------------------

????(1)???????:

????--->???????????????????????????????????????????.

????--->???????????????????????????????μ????.??:????????????Щ???????????????μ???????.

????(2)???????:

????-->?????????????????????????

????-->???????????????????????????

????-->???????????У?????????????????????????μ??????

????????:

????????
abstract public class Subject
{
abstract public void request();
}
????????
public class RealSubject extends Subject
{
public void request()
{
System.out.println("From real subject.");
}
}
????????
public class ProxySubject extends Subject
{
private RealSubject realSubject;  //???????????????????????
public ProxySubject()
{ realSubject=new RealSubject(); }
public void request()  //????????????
{
preRequest();
realSubject.request();  //??????????????request????
postRequest();
}
private void preRequest()
{
//something you want to do before requesting
}
private void postRequest()
{
//something you want to do after requesting
}
}
?????????
Subject sub=new ProxySubject();
Sub.request();

????(3)?????????

????Java?????????λ??Java.lang.reflect???£????????漰????????????

????1)Interface InvocationHandler???y???н????????????????invoke(Object obj??Method method?? Object[] args)??????????????????????obj????????????method?????????????args??÷???????????顣?????????????????ж??????

????2)Proxy????????????????????????????????????

????Static Object newProxyInstance(ClassLoader loader?? Class[] interfaces?? InvocationHandler h)???????????????????????????????????????????????á?

??????νDynamic Proxy?????????class??????????????????class?????????????????????interface??????????class?????????????Щ interface??

????3)??????????????????????????InvocationHandler???

public interface Subject
{
public void request();
}

??????????RealSubject??????

????????????

import java.lang.reflect.Method;
import java.lang.reflect.InvocationHandler;
public class DynamicSubject implements InvocationHandler {
private Object sub;
public DynamicSubject(Object obj) {
sub = obj;
}
public Object invoke(Object proxy?? Method method?? Object[] args) throws Throwable {
System.out.println("before calling " + method);
method.invoke(sub??args);
System.out.println("after calling " + method);
return null;
}
}
==>
method.invoke(sub??args);

???????????????????????????е??????????????sub??????????????args???б?????????????????

???????????????????????????????????????????????Щ????????