java单例模式

来源:互联网 发布:最新手游服务端源码 编辑:程序博客网 时间:2024/06/10 07:26
/**      * 单例模式创新!google的ioc作者写的。只有在调用的时候才会初始化!而且线程安全      * 超级牛!      *      */        public class Singleton {             static class SingletonHolder {                 static Singleton instance = new Singleton();             }             public static Singleton getInstance() {                 return SingletonHolder.instance;             }     }    


public class Singleton {             private static  Singleton instance = new Singleton();             private Singleton(){             }             public static Singleton getInstance(){                 return instance;             }         }    

import java.util.Vector;    public class GlobalConfig {            private static GlobalConfig instance = null;            private Vector properties = null;            private GlobalConfig() {          //Load configuration information from DB or file          //Set values for properties          }                private static synchronized void syncInit() {          if (instance == null) {              instance = new GlobalConfig();          }      }                public static GlobalConfig getInstance() {          if (instance == null) {              syncInit();          }                return instance;      }            public Vector getProperties() {          return properties;      }        }