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.
相关推荐
请考虑构建器)Item 3: Enforce the singleton property with a private constructor or an enum type(使用私有构造函数或枚举类型实施单例属性)第 4 条使用私有构造函数强制不可实例化(用初始化构造函数实施不可...
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 ...
官方版本,亲测可用
(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 ...
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 ...
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, ...
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 ...
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 ...
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...
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...
- **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 ...
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 ...
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 ...
**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...