`
leonzhx
  • 浏览: 798103 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 3: Enforce the singleton property with a private constructor or an enum

阅读更多

1.    Making a class a singleton can make it difficult to test its clients, as it’s impossible to substitute a mock implementation for a singleton unless it implements an interface that serves as its type.


2.    A privileged client can invoke the private constructor reflectively with the aid of the AccessibleObject.setAccessible method. If you need to defend against this attack, modify the constructor to make it throw an exception if it’s asked to create a second instance.

 

3.    To make a singleton class (not implemented as enum) serializable, it is not sufficient merely to add implements Serializable to its declaration. To maintain the singleton guarantee, you have to declare all instance fields transient and provide a readResolve method. Otherwise, each time a serialized instance is deserialized, a new instance will be created:

// readResolve method to preserve singleton property
private Object readResolve() {
// Return the one true Elvis and let the garbage collector
// take care of the Elvis impersonator.
return INSTANCE;
}

commented by Sean: I don't think we should just make all fields transient, because singleton doesn't mean immutable, we may recover singleton's fields from serialization, otherwise it doesn't make sense to make the singleton serializable.

 

4.    As of release 1.5, there is a third approach to implementing singletons. Simply make an enum type with one element. Besides that it is more concise, it provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. A single-element enum type is the best way to implement a singleton.

分享到:
评论

相关推荐

    effective Java(第3版)各章节的中英文学习参考(已完成).zip

    请考虑构建器)Item 3: Enforce the singleton property with a private constructor or an enum type(使用私有构造函数或枚举类型实施单例属性)第 4 条使用私有构造函数强制不可实例化(用初始化构造函数实施不可...

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 3: Enforce the singleton property with a private constructor or an enum type Item 4: Enforce noninstantiability with a private constructor Item 5: Prefer dependency injection to hardwiring ...

    access-modifier-checker-1.0-sources.jar

    官方版本,亲测可用

    Google C++ Style Guide(Google C++编程规范)高清PDF

    (One exception is if an argument Foo or const Foo& has a non-explicit, one-argument constructor, in which case we need the full definition to support automatic type conversion.) We can declare ...

    A Guide to the Project Management Body of Knowledge

    PMI has no power, nor does it undertake to police or enforce compliance with the contents of this document. PMI does not certify, tests, or inspect products, designs, or installations for safety or ...

    Vagaa哇嘎画时代--体验群体智慧的力量!

    6.2 As a condition to use the Software, you agree that you must not use the Software to infringe the intellectual property or other rights of others, in any way. The unauthorised reproduction, ...

    TeeChart2013_131216_SourceCode

    3) a license to use and/or distribute the Developed Software on a Network Server (the "Web Server License"). All of these licenses (individually and collectively, the "Licenses") are explained and ...

    TeeChart2013_130818_SourceCode

    3) a license to use and/or distribute the Developed Software on a Network Server (the "Web Server License"). All of these licenses (individually and collectively, the "Licenses") are explained and ...

    BlueToolInstall

    means a written, legally enforceable agreement that (i) stipulates that the Software is licensed, not sold, and that title to and ownership of the Software and any portion thereof remain with Broadcom...

    Recover4all Professional

    If You are a company or other form of organization, and You wish to authorize the Registered Software to be used by more than one (1) individual, You must purchase an additional license for each such...

    oracle 10g-admin1 考试试题

    - **Size of an Undo Block (A)**: The size of an undo block is crucial as it directly affects the amount of space required to store undo records. Larger undo blocks can hold more changes, potentially ...

    AZ-900.136Q.Pdf

    Private Cloud: A dedicated cloud environment managed either by the organization itself or a third party, providing exclusive access to the organization's resources. C. Hybrid Cloud: Combining both ...

    Jenkins Essentials.pdf

    If you are a Jenkins novice or beginner with a basic understanding of continuous integration, then this is the book for you. Beginners in Jenkins will get quick hands-on experience and gain the ...

    java英文笔试

    **Answer**: An abstract class cannot be instantiated directly because it contains one or more abstract methods, and Java does not provide a constructor for abstract classes. When you attempt to create...

Global site tag (gtag.js) - Google Analytics