特殊的静态代理模式,允许对象组合实现与继承相同的代码重用。
    代理模式注重过程
    委派模式注重结果

    框架
    JDK加载类时使用双亲委派模型
    一个类加载器在加载类时,先把这个请求委派给自己的父类加载器去执行。如果父类加载器还存在父类加载器,则继续向上委派,直到顶层的启动类加载器;如果父类加载器能够完成类加载,则成功返回;如果父类加载器无法完成加载,则子加载器尝试自己去加载

    1. abstract class ClassLoader{
    2. protect ClassLoader parent;
    3. protected Class<?> loadClass(String name, boolean resolve)
    4. throws ClassNotFoundException{
    5. synchronized (getClassLoadingLock(name)) {
    6. // First, check if the class has already been loaded
    7. Class<?> c = findLoadedClass(name);
    8. if (c == null) {
    9. long t0 = System.nanoTime();
    10. try {
    11. if (parent != null) {
    12. c = parent.loadClass(name, false);
    13. } else {
    14. c = findBootstrapClassOrNull(name);
    15. }
    16. } catch (ClassNotFoundException e) {
    17. // ClassNotFoundException thrown if class not found
    18. // from the non-null parent class loader
    19. }
    20. if (c == null) {
    21. // If still not found, then invoke findClass in order
    22. // to find the class.
    23. long t1 = System.nanoTime();
    24. c = findClass(name);
    25. // this is the defining class loader; record the stats
    26. sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
    27. sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
    28. sun.misc.PerfCounter.getFindClasses().increment();
    29. }
    30. }
    31. if (resolve) {
    32. resolveClass(c);
    33. }
    34. return c;
    35. }
    36. }
    37. }