所谓“懒汉式”与“饿汉式”的区别,是在与建立单例对象的时间的不同。
“懒汉式”是在你真正用到的时候才去建这个单例对象:
比如:有个单例对象
public class Singleton{ 
    private Singleton(){}
    private static Singleton singleton = null;  //不建立对象
    public static synchronized Singleton getInstance(){
             if(singleton == null) {        //先判断是否为空
                 singleton = new Singleton ();  //懒汉式做法 
             }
             return singleton ;
     }
}
 “饿汉式”是在不管你用的用不上,一开始就建立这个单例对象:比如:有个单例对象
public class Singleton{ 
    public Singleton(){}
    private static Singleton singleton = new Singleton();  //建立对象
    public static Singleton getInstance(){
  return singleton ;//直接返回单例对象 }}

它有以下几个要素:

  • 私有的构造方法
  • 指向自己实例的私有静态引用
  • 以自己实例为返回值的静态的公有的方法

相关文章:

  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
  • 2022-01-05
  • 2022-02-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-09
  • 2021-07-16
  • 2021-08-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-30
相关资源
相似解决方案