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;
??? }
???
?