?????????????????????????????Java?ж???????????????????????????????????????????????????????????????????????????????????????????????лл??????
???????????
????1. ???????
??????????????????????????????泧?????????????“???”???????????????????????????????????????????????????????????????????????????????“?????”????????????????????Σ??????????????????????????????????????????????ζ???????“????”???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????????????????????????????
???????????????????????????????????????????????????????????Щ?????????
????2. ???????
??????????????????????????????????????????????????????????????μ???????????????????Java?????ж????? ???????£?????????е?????????????????????????????????????????????????Vendor??????????????BusinessAgent????????????????????????????????????????????????Sell????Sell??????????£?
????public interface Sell {
????void sell();
????void ad();
????}
????Vendor?????????£?
????public class Vendor implements Sell {
????public void sell() {
????System.out.println("In sell method");
????}
????public void ad() {
????System??out.println("ad method")
????}
????}
??????????BusinessAgent????????£?
????public class BusinessAgent implements Sell {
????private Vendor mVendor;
????public BusinessAgent(Vendor vendor) {
????mVendor = vendor;
????}
????public void sell() { mVendor.sell(); }
????public void ad() { mVendor.ad(); }
????}
??????BusinessAgent??????????????????????????????????????????????????????????????ü??ɡ?
???????????????????????????Vendor????????????????????????????????????????????????????Vendor??????????????????BusinessAgent???е?sell?????????????ж?????????????
????public class BusinessAgent implements Sell {
????...
????public void sell() {
????if (isCollegeStudent()) {
????vendor.sell();
????}
????}
????...
????}
???????????????????????????????????????????????????????????????????????????????????Щ??????????????????????????????????д???????????????????????????????????????????????????
???????????????
????1. ??????????
??????????????????????????????????????????????????????????£?????????????Java?????ж??????????????????????????Java?????е?“??”???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
???????????????????????????????????????????е?????????“before”??????????????“after”????????????????????е?Vendor??????????BusinessAgent????????????????н????????????????t???????????????????????????£?
public class BusinessAgent implements Sell {
private Vendor mVendor;
public BusinessAgent(Vendor vendor) {
this.mVendor = vendor;
}
public void sell() {
System.out.println("before");
mVendor.sell();
System.out.println("after");
}
public void ad() {
System.out.println("before");
mVendor.ad();
System.out.println("after");
}
}
??????????????????????????????????????????????????????????????????ж???????????????????????????????????????????????????Sell????а?????????????????????t????????д???????????????????????????????????“????”??????????д??????????????????????????????????????????????????????????????????????????????????????
????2. ?????????
??????1??InvocationHandler???
????????????????????????????????λ???????????????????н???????н?????????InvocationHandler?????????????????£?
????public interface InvocationHandler {
????Object invoke(Object proxy?? Method method?? Object[] args);
????}
??????InvocationHandler????????????????????????????????н???????“?????????”?????????????????????????????“????”??????invoke?????У?????????????proxy????????????method?????????????????????????????????args???????????????????????????????????е????з???????????????invoke???????????????????invoke?????????????????????????????method???????????????????????????????????????????н????invoke????????????“before”??????????????invoke???????????“after”?????????????????????????????
??????2???????????
??????????????£????????????????????????????????????Sell?????????Vendor?????????£?
????public class Vendor implements Sell {
????public void sell() {
????System.out.println("In sell method");
????}
????public void ad() {
????System??out.println("ad method")
????}
????}
??????3???н???
???????????????????н?????????InvocationHandler????????????????”????“?????????????á??н??????????£?
public class DynamicProxy implements InvocationHandler {
private Object obj; //obj?????????
public DynamicProxy(Object obj) {
this.obj = obj;
}
@Override
public Object invoke(Object proxy?? Method method?? Object[] args) throws Throwable {
System.out.println("before");
Object result = method.invoke(obj?? args);
System.out.println("after");
return result;
}
}
????????????????????????????н????????????????????????invoke?????е?????????????????????????11?У???????????????????????????????????????????????????????????invoke???????????????????????á?????????????????????????????????????????н?????????????????????????????????У??н??????????????????????
?????????????н???????????????????????????????У??н??????????????????????????????????????????????????????????????????????????????????????????”??“?????????????
??????4??????????????
??????????????????????????£?
public class Main {
public static void main(String[] args) {
//?????н??????
DynamicProxy  inter = new DynamicProxy(new Vendor());
//??????佫????????$Proxy0.class??????????????????????????????
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles"??"true");
//????????????sell
Sell sell = (Sell)(Proxy.newProxyInstance(Sell.class.getClassLoader()??
new Class[] {Sell.class}?? inter));
//?????????????????????????????????invoke????????
sell.sell();
sell.ad();
}
}
??????????????У????????Proxy???newProxyInstance???????????????????????????????????????????????????????????÷???????????????????????????????????£?
????public static Object newProxyInstance(ClassLoader loader?? Class<?>[] interfaces?? InvocationHandler h) throws IllegalArgumentException