JDK源代码中的Singleton模式

    在jdk源代码中存在着很多的设计模式,在这里找出jdk源代码中设计模式存在的形式,便于自己学习,也希望对别人有些帮助,希望大家指点:
    1.Singleton模式以下的jdk源代码中存在:
    A.java.lang.Runtime]类,是一个单例类,通过预加载产生Runtime实例: private static Runtime currentRuntime = new Runtime();通过getRuntime方法取得单例类:
    public static Runtime getRuntime() {
    return currentRuntime;
     }
    B.java.awt.Desktop#getDesktop()
    public static synchronized Desktop getDesktop(){
        if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
        if (!Desktop.isDesktopSupported()) {
            throw new UnsupportedOperationException("Desktop API is not " +
                                           "supported on the current platform");
        }

        sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
        Desktop desktop = (Desktop)context.get(Desktop.class);

        if (desktop == null) {
            desktop = new Desktop();
            context.put(Desktop.class, desktop);

        }
       
        return desktop;
    }
    C.java.awt.Toolkit#getDefaultToolkit()
    public static synchronized Toolkit getDefaultToolkit() {
        if (toolkit == null) {
            try {
                // We disable the JIT during toolkit initialization.  This
                // tends to touch lots of classes that aren't needed again
                // later and therefore JITing is counter-productiive.
                java.lang.Compiler.disable();
               
                java.security.AccessController.doPrivileged(
                        new java.security.PrivilegedAction() {
                    public Object run() {
                        String nm = null;
                        Class cls = null;
                        try
                nm = System.getProperty("awt.toolkit", "sun.awt.X11.XToolkit");
                            try {
                                cls = Class.forName(nm);
                            } catch (ClassNotFoundException e) {
                ClassLoader cl = ClassLoader.getSystemClassLoader();
                                if (cl != null) {
                                    try {
                                        cls = cl.loadClass(nm);
                                    } catch (ClassNotFoundException ee) {
                                        if (GraphicsEnvironment.isHeadless()) {
                                            nm = "sun.awt.HToolkit";
                                            try {
                                                cls = Class.forName(nm);
                                            } catch (ClassNotFoundException xe) {
                                                try {
                                                    cls = cl.loadClass(nm);
                                         } catch (ClassNotFoundException xee) {
                                   throw new AWTError("Toolkit not found: " + nm);
                                                }
                                            }
                                        }
                                        else
                           throw new AWTError("Toolkit not found: " + nm);
                                    }
                                }
                            }
                            if (cls != null) {
                                toolkit = (Toolkit)cls.newInstance();
                                if (GraphicsEnvironment.isHeadless()) {
                                    toolkit = new HeadlessToolkit(toolkit);
                                }
                            }
                        } catch (InstantiationException e) {
                       throw new AWTError("Could not instantiate Toolkit: " + nm);
                        } catch (IllegalAccessException e) {
                            throw new AWTError("Could not access Toolkit: " + nm);
                        }
                        return null;
                    }
                });
                loadAssistiveTechnologies();
            } finally {
                // Make sure to always re-enable the JIT.
                java.lang.Compiler.enable();
            }
        }
        return toolkit;
    }
    D.java.awt.GraphicsEnvironment#getLocalGraphicsEnvironment()
        public static synchronized GraphicsEnvironment getLocalGraphicsEnvironment() {
if (localEnv == null) {
    String nm = (String) java.security.AccessController.doPrivileged
(new sun.security.action.GetPropertyAction
("java.awt.graphicsenv", null));

     try {// long t0 = System.currentTimeMillis();
             localEnv = (GraphicsEnvironment) Class.forName(nm).newInstance();
             // long t1 = System.currentTimeMillis();
             //System.out.println("GE creation took " + (t1-t0)+ "ms.");
                if (isHeadless()) {
                    localEnv = new HeadlessGraphicsEnvironment(localEnv);
                }
    } catch (ClassNotFoundException e) {
                throw new Error("Could not find class: "+nm);
            } catch (InstantiationException e) {
        throw new Error("Could not instantiate Graphics Environment: "+ nm);
            } catch (IllegalAccessException e) {
                throw new Error ("Could not access Graphics Environment: "+ nm);
            }
        }

return localEnv;
    }

   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值