| Start |
|---|
Enum Singleton Enums (opens new window) have private constructors, are final and provide proper serialization machinery. They are also very concise and lazily initialized in a thread safe manner. The JVM provides a guarantee that enum values will not be instantiated more than once each, which gives the enum singleton pattern a very strong defense against reflection attacks. What the enum pattern doesn't protect against is other developers physically adding more elements to the source code. Consequently, if you choose this implementation style for your singletons it is imperative that you very clearly document that no new values should be added to those enums. This is the recommended way of implementing the singleton pattern, as explained (opens new window) by Joshua Bloch in Effective Java. |
Singleton without use of Enum (eager initialization) It can be argued that this example is effectively lazy initialization. Section 12.4.1 of the Java Language Specification (opens new window) states: A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
Therefore, as long as there are no other static fields or static methods in the class, the |
Thread-safe lazy initialization using holder class | Bill Pugh Singleton implementation This initializes the This implementation is also known as Bill Pugh singleton pattern. [Wiki] (opens new window) |
| Thread safe Singleton with double checked locking This type of Singleton is thread safe, and prevents unnecessary locking after the Singleton instance has been created. It must be emphasized -- in versions prior to Java SE 5, the implementation above is incorrect (opens new window) and should be avoided. It is not possible to implement double-checked locking correctly in Java prior to Java 5. |
| Extending singleton (singleton inheritance) In this example, base class It's subclasses |
Read Article
https://develop.horje.com/learn/1434/reference