class Singleton1{ private Singleton1(){} private static Singleton1 s1 = new Singleton1(); public static Singleton1 getSingleton(){ return s1; } }
class Singleton2{ private Singleton2(){} private static Singleton2 s2 = null; public static Singleton2 getSingleton2(){ if(s2 == null){ s2 = new Singleton2(); } return s2; } }
class Singleton3{ private Singleton3(){} private static class SingleHolder{ private final static Singleton3 s3 = new Singleton3(); } public static Singleton3 getSingleton3(){ return SingleHolder.s3; } }
class Singleton4{ private static Singleton4 s4= null; private Singleton4(){} public static synchronized Singleton4 getSingleton4(){ if(s4 == null){ s4 = new Singleton4(); } return s4; } }
原文地址:http://blog.csdn.net/simongeek/article/details/45245061