特殊的静态代理模式,允许对象组合实现与继承相同的代码重用。
代理模式注重过程
委派模式注重结果
框架
JDK加载类时使用双亲委派模型
一个类加载器在加载类时,先把这个请求委派给自己的父类加载器去执行。如果父类加载器还存在父类加载器,则继续向上委派,直到顶层的启动类加载器;如果父类加载器能够完成类加载,则成功返回;如果父类加载器无法完成加载,则子加载器尝试自己去加载
abstract class ClassLoader{protect ClassLoader parent;protected Class<?> loadClass(String name, boolean resolve)throws ClassNotFoundException{synchronized (getClassLoadingLock(name)) {// First, check if the class has already been loadedClass<?> c = findLoadedClass(name);if (c == null) {long t0 = System.nanoTime();try {if (parent != null) {c = parent.loadClass(name, false);} else {c = findBootstrapClassOrNull(name);}} catch (ClassNotFoundException e) {// ClassNotFoundException thrown if class not found// from the non-null parent class loader}if (c == null) {// If still not found, then invoke findClass in order// to find the class.long t1 = System.nanoTime();c = findClass(name);// this is the defining class loader; record the statssun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);sun.misc.PerfCounter.getFindClasses().increment();}}if (resolve) {resolveClass(c);}return c;}}}
