首页 > 编程语言 > 详细

Effective Java 英文 第二版 读书笔记 Item 3:Enforce the singleton property with a private constructor or an enum type.

时间:2015-09-04 07:12:20      阅读:351      评论:0      收藏:0      [点我收藏+]

Making a class a singleton can make it difficult to test clients.

 

package singletonProperty;

//ingleton with public final field 
public class ElvisField {
    public static final ElvisField INSTANCE=new ElvisField();
    private ElvisField(){
        
    }
    
}

 

package singletonProperty;

//Singleton with static factory 
public class ElvisMethod {
    
    private static final ElvisMethod INSTANCE=new ElvisMethod();
    
    private ElvisMethod(){
        
    }
    
    public static ElvisMethod getInstance() {
        return INSTANCE;
    }
    
}

 

package singletonProperty;

//Enum singleton - the preferred approach
public enum ElvisEnum {
    INSTANCE;
}

 

A single-element enum type is the best way to implement a singleton

Effective Java 英文 第二版 读书笔记 Item 3:Enforce the singleton property with a private constructor or an enum type.

原文:http://www.cnblogs.com/linkarl/p/4780957.html

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