java ???????????2??
???????????? ???????[ 2013/8/6 10:02:38 ] ????????
????------------------------------ ??????(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???б?????????????????
???????????????????????????????????????????????Щ????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11