java 单例模式模式实现

时间:2021-03-06 14:58:12   收藏:0   阅读:0

  参考:https://www.cnblogs.com/ngy0217/p/9006716.html

饿汉式(线程安全,调用效率高,但不能延时加载):

       

public class ImageLoader{ 
     private static ImageLoader instance = new ImageLoader; 
     private ImageLoader(){} 
     public static ImageLoader getInstance(){  
          return instance;  
      } 
}

  懒汉式:静态内部类实现模式(线程安全,调用效率高,可以延时加载)

public class SingletonDemo3 {
     
      private static class SingletonClassInstance{
          private static final SingletonDemo3 instance=new SingletonDemo3();
     }
      
     private SingletonDemo3(){}
    
     public static SingletonDemo3 getInstance(){
         return SingletonClassInstance.instance;
     }
}

  

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!