/** *懶漢式 */ public class Singleton{ private static Singleton singleton = new Singleton(); private Singleton(){} public static Singleton getSingleton(){ return singleton; } }
/** * 餓漢式 */ public class Singleton{ private static Singleton singleton = null; private Singleton(){} public static synchronized Singleton getSingleton(){ if(singleton==null){ singleton = new Singleton(); } return singleton; } }