标签:
package com.doctor.design_pattern.singleton; /** * @author sdcuike * * Created on 2016年7月31日 下午11:36:05 * * 饿汉式 单例 * EagerInitialized Singleton */ public class EagerInitializedSingleton { private static final EagerInitializedSingleton instance = new EagerInitializedSingleton(); private EagerInitializedSingleton() { } public static EagerInitializedSingleton getInstance() { return instance; } public void doSomething() { System.out.println("test"); } public static void main(String[] args) { System.out.println(EagerInitializedSingleton.getInstance()); System.out.println(EagerInitializedSingleton.getInstance()); EagerInitializedSingleton.getInstance().doSomething(); // com.doctor.design_pattern.singleton.EagerInitializedSingleton@2a139a55 // com.doctor.design_pattern.singleton.EagerInitializedSingleton@2a139a55 // test System.out.println(EagerInitializedSingleton.getInstance() == EagerInitializedSingleton.getInstance()); // true } }
package com.doctor.design_pattern.singleton; /** * @author sdcuike * * Created on 2016年8月1日 上午12:30:08 */ public class StaticBlockSingleton { private static StaticBlockSingleton instance; public static StaticBlockSingleton getInstance() { return instance; } static { try { instance = new StaticBlockSingleton(); } catch (Exception e) { throw new RuntimeException("Exception occured in creating singleton instance"); } } public static void main(String[] args) { System.out.println(StaticBlockSingleton.getInstance()); System.out.println(StaticBlockSingleton.getInstance()); System.out.println(StaticBlockSingleton.getInstance() == StaticBlockSingleton.getInstance()); // com.doctor.design_pattern.singleton.StaticBlockSingleton@2a139a55 // com.doctor.design_pattern.singleton.StaticBlockSingleton@2a139a55 // true } }
package com.doctor.design_pattern.singleton; /** * @author sdcuike * * Created on 2016年8月1日 上午12:39:40 */ public class LazyInitializedSingleton { private static LazyInitializedSingleton instance; public static LazyInitializedSingleton getInstance() { if (instance == null) { instance = new LazyInitializedSingleton(); } return instance; } private LazyInitializedSingleton() { } public static void main(String[] args) { System.out.println(LazyInitializedSingleton.getInstance() == LazyInitializedSingleton.getInstance());// true } }
synchronized
package com.doctor.design_pattern.singleton; /** * @author sdcuike * * Created on 2016年8月1日 上午11:19:36 * * Thread Safe Singleton * */ public class ThreadSafeSingleton { private static ThreadSafeSingleton instance; public static synchronized ThreadSafeSingleton getInstance() { if (instance == null) { instance = new ThreadSafeSingleton(); } return instance; } private ThreadSafeSingleton() { } public static void main(String[] args) { System.out.println(ThreadSafeSingleton.getInstance() == ThreadSafeSingleton.getInstance()); // true } }
package com.doctor.design_pattern.singleton; /** * @author sdcuike * * Created on 2016年8月1日 上午11:37:25 * * double checked locking principle */ public class DoubleCheckedLockingThreadSafeSingleton { private static DoubleCheckedLockingThreadSafeSingleton instance; public static DoubleCheckedLockingThreadSafeSingleton getInstance() { if (instance == null) { synchronized (DoubleCheckedLockingThreadSafeSingleton.class) { if (instance == null) { instance = new DoubleCheckedLockingThreadSafeSingleton(); } } } return instance; } private DoubleCheckedLockingThreadSafeSingleton() { } public static void main(String[] args) { } }
package com.doctor.design_pattern.singleton; /** * @author sdcuike * * Inner static helper class Singleton * * * Created on 2016年8月1日 上午11:54:05 */ public class InnerStaticHelperClassSingleton { private InnerStaticHelperClassSingleton() { } public static InnerStaticHelperClassSingleton getInstance() { return SingletonHelper.instance; } private static class SingletonHelper { private static final InnerStaticHelperClassSingleton instance = new InnerStaticHelperClassSingleton(); } public static void main(String[] args) { } }
package com.doctor.design_pattern.singleton; /** * @author sdcuike * * Enum Singleton * 枚举实现单例不能延迟加载资源,但保证了enum值只实例化一次。而且克服了反射带来的问题 * * Created on 2016年8月1日 下午12:24:06 */ public enum EnumSingleton { instance; private EnumSingleton() { } public void doSomething() { System.out.println("test do "); } public static void main(String[] args) { EnumSingleton.instance.doSomething(); } }
protected Object readResolve() { return getInstance(); }
java设计模式之单例模式(Singleton pattern)
标签:
原文地址:http://blog.csdn.net/doctor_who2004/article/details/52081308