Singleton指仅仅被实例化一次的类
public class Elvis {
public static final Elvis INSTANCE = new Elvis();
private Elvis() {
...
}
public void leaveTheBuilding() {
...
}
}
public class Elvis {
private static final Elvis INSTANCE = new Elvis();
private Elvis() {
...
}
public static Elvis getInstance() {
return INSTANCE;
}
public void leaveTheBuilding() {
...
}
}
单元素枚举类型更加简洁,提供了序列化机制,绝对防止多次实例化
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() {
...
}
}
单元素的枚举类型已经成为实现Singleton的最佳方法
Relative Post—Java设计模式之singleton