singleton模式的实现方法有很多种么?
除了板桥写的,我在developerwork上还看到了用静态方法和静态变量实现
Singleton模式,另外,theserverside上也有。
就拿板桥的
public class Singleton {
private static Singleton instance = null;
public static synchronized Singleton getInstance() {
//这个方法比上面有所改进,不用每次都进行生成对象,只是第一次
//使用时生成实例,提高了效率!
if (instance==null)
instance=new Singleton();
return instance; }
}
developerworks上类似的为
public class Singleton {
private static Singleton s;
private Singleton(){};
/**
* Class method to access the singleton instance of the class.
*/
public static Singleton getInstance() {
if (s == null)
s = new Singleton();
return s;
}
}
不知道现在究竟有多少种定义,是不是可以自己修改,哪几种是比较为大家公认的。
我还想知道Singleton的最原始的定义是什么,我知道自己还没有理解这个模式的本质,各位朋友帮帮忙吧,谢谢