Skip to content

Commit

Permalink
Merge pull request iluwatar#19 from Alwayswithme/master
Browse files Browse the repository at this point in the history
enum approach of singleton
  • Loading branch information
iluwatar committed Nov 26, 2014
2 parents 13e6c74 + 11a89f8 commit b73429c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions singleton/src/main/java/com/iluwatar/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Singleton pattern ensures that the class (IvoryTower) can have only one
* existing instance and provides global access to that instance.
*
* http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java
*
*/
public class App {

Expand All @@ -22,5 +24,9 @@ public static void main(String[] args) {
System.out.println("threadSafeIvoryTower1=" + threadSafeIvoryTower1);
System.out.println("threadSafeIvoryTower2=" + threadSafeIvoryTower2);

EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
System.out.println("enumIvoryTower1=" + enumIvoryTower1);
System.out.println("enumIvoryTower2=" + enumIvoryTower2);
}
}
15 changes: 15 additions & 0 deletions singleton/src/main/java/com/iluwatar/EnumIvoryTower.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.iluwatar;

/**
*
* Enum Singleton class.
*
*/
public enum EnumIvoryTower {
INSTANCE;

@Override
public String toString() {
return getDeclaringClass().getCanonicalName() + "@" + hashCode();
}
}

0 comments on commit b73429c

Please sign in to comment.