|
22 | 22 | */
|
23 | 23 | package com.iluwatar.singleton;
|
24 | 24 |
|
25 |
| -import java.io.Serializable; |
26 |
| - |
27 | 25 | /**
|
28 |
| - * The Initialize-on-demand-holder idiom is a secure way of creating lazy initialized singleton |
29 |
| - * object in Java. refer to "The CERT Oracle Secure Coding Standard for Java" By Dhruv Mohindra, |
30 |
| - * Robert C. Seacord p.378 |
31 |
| - * <p/> |
32 |
| - * Singleton objects usually are heavy to create and sometimes need to serialize them. This class |
33 |
| - * also shows how to preserve singleton in serialized version of singleton. |
| 26 | + * The Initialize-on-demand-holder idiom is a secure way of creating a lazy initialized singleton |
| 27 | + * object in Java. |
| 28 | + * <p> |
| 29 | + * The technique is is as lazy as possible and works in all known versions of Java. It takes advantage |
| 30 | + * of language guarantees about class initialization, and will therefore work correctly in all |
| 31 | + * Java-compliant compilers and virtual machines. |
| 32 | + * <p> |
| 33 | + * The inner class is referenced no earlier (and therefore loaded no earlier by the class loader) than |
| 34 | + * the moment that getInstance() is called. Thus, this solution is thread-safe without requiring special |
| 35 | + * language constructs (i.e. volatile or synchronized). |
34 | 36 | *
|
35 |
| - * @author mortezaadi@gmail.com |
36 | 37 | */
|
37 |
| -public final class InitializingOnDemandHolderIdiom implements Serializable { |
38 |
| - |
39 |
| - private static final long serialVersionUID = 1L; |
| 38 | +public final class InitializingOnDemandHolderIdiom { |
40 | 39 |
|
| 40 | + /** |
| 41 | + * Private constructor. |
| 42 | + */ |
41 | 43 | private InitializingOnDemandHolderIdiom() {}
|
42 | 44 |
|
| 45 | + /** |
| 46 | + * @return Singleton instance |
| 47 | + */ |
43 | 48 | public static InitializingOnDemandHolderIdiom getInstance() {
|
44 | 49 | return HelperHolder.INSTANCE;
|
45 | 50 | }
|
46 | 51 |
|
47 |
| - protected Object readResolve() { |
48 |
| - return getInstance(); |
49 |
| - } |
50 |
| - |
| 52 | + /** |
| 53 | + * Provides the lazy-loaded Singleton instance. |
| 54 | + */ |
51 | 55 | private static class HelperHolder {
|
52 | 56 | public static final InitializingOnDemandHolderIdiom INSTANCE =
|
53 | 57 | new InitializingOnDemandHolderIdiom();
|
54 | 58 | }
|
55 |
| - |
56 | 59 | }
|
0 commit comments