0
点赞
收藏
分享

微信扫一扫

单例模式(懒汉,饿汉)


<pre name="code" class="java"><span style="font-size:18px;"></span><pre name="code" class="java" style="color: rgb(54, 46, 43); line-height: 26px;"><span style="font-size:18px;">单例有以下几个要素: 私有的构造方法 指向自己实例的私有静态引用 以自己实例为返回值的静态的公有的方法</span>



<span style="font-size:18px;">所谓“懒汉式”与“饿汉式”的区别,是在与建立单例对象的时间的不同。
“懒汉式”是在你真正用到的时候才去建这个单例对象:
比如:有个单例对象
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 //直接返回单例对象 }</span>

<span style="font-size:18px;">}

</span>





举报

相关推荐

0 条评论