singleton 的第三种形式
刚才在浏览设计模式的分析时谈到了singleton的两种形式,我有时采用下面的这种形式,算不算第三种方式呢?提出来供大家讨论
public class Singleton {
private static Singleton instance = null;
//只在第一次被引用时生成,效率与第二种形式是一样的
static {
instance = new Singleton();
}
static Singleton getInstance() {
return instance;
}
}