看Eclipse JFace Table文档时发现Singleton---jdk1.6开始后的改变
As of Java 6 you can singletons with a single-element enum type. This way is currently the best way to implement a singleton in Java 1.6 or later according to tht book ""Effective Java from Joshua Bloch.
package mypackage;
public enum MyEnumSingleton {
INSTANCE;
// other useful methods here
}
Before Java 1.6 a class which should be a singleton can be defined like the following.
public class Singleton {
private static Singleton uniqInstance;
private Singleton() {
}
public static synchronized Singleton getInstance() {
if (uniqInstance == null) {
uniqInstance = new Singleton();
}
return uniqInstance;
}
// other useful methods here
}
参考:http://www.vogella.de/articles/EclipseJFaceTable/article.html
http://www.vogella.de/articles/DesignPatternSingleton/article.html