单例模式

饿汉式

 1 public static class Single{
 2 
 3     //直接创建对象
 4     public static Single instance=new Single();
 5 
 6     //私有化对象
 7     private Single(){
 8     }
 9 
10     //返回对象实例
11     public static Single getInstance(){
12         return instance;
13     }
14 }

懒汉式

public static class Single {

//首先声明变量
public static Single instance=null;

//私有化对象
private Single(){
}

//提供对外方法
public static Single getInstance(){
if(instance==null){
      synchronized(instance.calss){
      if(instance==null){
instance =new instance();
       }
     }
}
return instance;
}

 
原文地址:https://www.cnblogs.com/rzqz/p/7305072.html