标签:singleton 饿汉 懒汉式 hung one 分享图片 opened alt view
饿汉式:
1 /** 2 * 单例模式 饿汉式 3 */ 4 public class SingletonHungry { 5 6 private SingletonHungry (){ 7 } 8 public static SingletonHungry instance = new SingletonHungry(); 9 10 public static SingletonHungry getInstance(){ 11 return instance; 12 } 13 14 }
懒汉式:
1 /** 2 * 单例模式 懒汉式 3 */ 4 public class Singleton { 5 6 private static class SingletonHolder{ 7 private static Singleton instance = new Singleton(); 8 } 9 10 private Singleton(){ 11 } 12 13 public static Singleton getInstance(){ 14 return SingletonHolder.instance; 15 } 16 }
标签:singleton 饿汉 懒汉式 hung one 分享图片 opened alt view
原文地址:https://www.cnblogs.com/sunny007/p/9743194.html