单例模式
作者:互联网
1,饿汉模式
1 //饿汉单例模式 2 public class SingleInstance01 { 3 public static SingleInstance01 instance = new SingleInstance01(); 4 5 private SingleInstance01() { 6 } 7 }
2,懒汉模式
1 public class SingleInstance02 { 2 public static SingleInstance02 instance02; 3 private SingleInstance02(){ 4 5 } 6 public static SingleInstance02 getInstance02(){ 7 if(instance02==null){ 8 instance02 = new SingleInstance02(); 9 } 10 return instance02; 11 } 12 }
标签:饿汉,instance02,模式,SingleInstance01,单例,SingleInstance02,static,public 来源: https://www.cnblogs.com/zhgsh/p/16369479.html