首页 > 编程语言 > 详细

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

时间:2015-02-25 14:09:51      阅读:255      评论:0      收藏:0      [点我收藏+]

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();

 

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

原文:http://www.cnblogs.com/yshyee/p/4299436.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!