Effective Java Item3:Enforce the singleton property with a private constructor or an enum type

Item3:Enforce the singleton property with a private constructor or an enum type

采用枚举类型(ENUM)实现单例模式。

public enum Elvis {
INSTANCE; public void leaveTheBuiding(){
System.out.println("a single-element enum type is the best way to implement a singleton");
}
}

使用方式:

Elvis.INSTANCE.leaveTheBuiding();
上一篇:linux概念之进程分析


下一篇:(转)mysql explain详解