您的位置:首页 > Web前端

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

2015-02-25 13:09 363 查看
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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐