码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-02-25 14:10:25      阅读:136      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!