code_junkie

Efficient way to implement singleton pattern in Java

Nov 10th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. public enum Foo {
  2. INSTANCE;
  3. }
  4.  
  5. public enum Elvis {
  6. INSTANCE;
  7. private final String[] favoriteSongs =
  8. { "Hound Dog", "Heartbreak Hotel" };
  9. public void printFavorites() {
  10. System.out.println(Arrays.toString(favoriteSongs));
  11. }
  12. }
  13.  
  14. public enum Foo {
  15. INSTANCE;
  16. }
  17.  
  18. public final class Foo {
  19.  
  20. private static final Foo INSTANCE = new Foo();
  21.  
  22. private Foo() {
  23. if (INSTANCE != null) {
  24. throw new IllegalStateException("Already instantiated");
  25. }
  26. }
  27.  
  28. public static Foo getInstance() {
  29. return INSTANCE;
  30. }
  31. }
  32.  
  33. public final class Foo {
  34.  
  35. private static class FooLoader {
  36. private static final Foo INSTANCE = new Foo();
  37. }
  38.  
  39. private Foo() {
  40. if (FooLoader.INSTANCE != null) {
  41. throw new IllegalStateException("Already instantiated");
  42. }
  43. }
  44.  
  45. public static Foo getInstance() {
  46. return FooLoader.INSTANCE;
  47. }
  48. }
  49.  
  50. public final class Foo implements Serializable {
  51.  
  52. private static final long serialVersionUID = 1L;
  53.  
  54. private static class FooLoader {
  55. private static final Foo INSTANCE = new Foo();
  56. }
  57.  
  58. private Foo() {
  59. if (FooLoader.INSTANCE != null) {
  60. throw new IllegalStateException("Already instantiated");
  61. }
  62. }
  63.  
  64. public static Foo getInstance() {
  65. return FooLoader.INSTANCE;
  66. }
  67.  
  68. @SuppressWarnings("unused")
  69. private Foo readResolve() {
  70. return FooLoader.INSTANCE;
  71. }
  72. }
  73.  
  74. class Foo {
  75. private static class BarHolder {
  76. public static Bar bar = new Bar();
  77. }
  78.  
  79. public static Bar getBar() {
  80. return BarHolder.bar;
  81. }
  82. }
  83.  
  84. class Foo {
  85. private static volatile Bar bar = null;
  86. public static Bar getBar() {
  87. if (bar == null) {
  88. synchronized(Foo.class) {
  89. if (bar == null)
  90. bar = new Bar();
  91. }
  92. }
  93. return bar;
  94. }
  95. }
  96.  
  97. public class A {
  98.  
  99. private static A singleton = new A();
  100.  
  101. private A() {}
  102.  
  103. public static A getInstance() {
  104. return singleton;
  105. }
  106. }
  107.  
  108. public class Singleton {
  109. private final static Singleton INSTANCE = new Singleton();
  110.  
  111. private Singleton() {}
  112.  
  113. public static Singleton getInstance() { return Singleton.INSTANCE; }
  114.  
  115. protected Object clone() {
  116. throw new CloneNotSupportedException();
  117. }
  118. }
  119.  
  120. public class Singleton {
  121. private static Singleton instance = null;
  122.  
  123. private Singleton() {}
  124.  
  125. public static Singleton getInstance() {
  126. if(null == instance) {
  127. synchronized(Singleton.class) {
  128. if(null == instance) {
  129. instance = new Singleton();
  130. }
  131. }
  132. }
  133. return instance;
  134. }
  135.  
  136. protected Object clone() {
  137. throw new CloneNotSupportedException();
  138. }
  139. }
  140.  
  141. public static final INSTANCE == ....
  142. private Object readResolve() {
  143. return INSTANCE; // original singleton instance.
  144. }
  145.  
  146. public class Singleton {
  147.  
  148. private static Singleton instance = null;
  149.  
  150. static {
  151. instance = new Singleton();
  152. // do some of your instantiation stuff here
  153. }
  154.  
  155. private Singleton() {
  156. if(instance!=null) {
  157. throw new ErrorYouWant("Singleton double-instantiation, should never happen!");
  158. }
  159. }
  160.  
  161. public static getSingleton() {
  162. return instance;
  163. }
  164.  
  165. }
Add Comment
Please, Sign In to add comment