在框架里,看到一段动态代理的混乱设计。可能后期会改的。中午要睡了,先贴出来,以后有时间再分析更改注释.
public interface Bird {
public void fly();
public void song();
}
public class TinyBird implements Bird {
private static final TinyBird instance = new TinyBird();
static final TinyBird getDefault() {
return instance;
}
@Override
public void fly() {
System.out.println("bird flying .");
}
@Override
public void song() {
System.out.println("bird songing .");
}
}
public class BirdHandler implements InvocationHandler {
private Object deledate;
public BirdHandler(TinyBird deledate) {
this.deledate = deledate;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
doBefore();
Object result = method.invoke(deledate, args);
doAfter();
return result;
}
private void doBefore() {
System.out.println("before.");
}
private void doAfter() {
System.out.println("after.");
}
}
public class Factory {
private static Bird instance = newInstance(TinyBird.getDefault());
public static Bird getDefaultSettings() {
return instance;
}
public static Bird newSettings() {
return newInstance(new TinyBird());
}
private static Bird newInstance(TinyBird real) {
Class<?> c = TinyBird.class;
return (Bird) Proxy.newProxyInstance(c.getClassLoader(),
c.getInterfaces(), new BirdHandler(real));
}
}
Factory.getDefaultSettings()和Factory.
newSettings()为什么要同时出现。。。。。。。