Merge pull request #19 from Alwayswithme/master

enum approach of singleton
This commit is contained in:
Ilkka Seppälä 2014-11-26 15:58:31 +02:00
commit b73429cd95
2 changed files with 21 additions and 0 deletions

View File

@ -5,6 +5,8 @@ package com.iluwatar;
* 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 {
@ -22,5 +24,9 @@ public class App {
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);
}
}

View File

@ -0,0 +1,15 @@
package com.iluwatar;
/**
*
* Enum Singleton class.
*
*/
public enum EnumIvoryTower {
INSTANCE;
@Override
public String toString() {
return getDeclaringClass().getCanonicalName() + "@" + hashCode();
}
}