Merge pull request #19 from Alwayswithme/master
enum approach of singleton
This commit is contained in:
commit
b73429cd95
@ -5,6 +5,8 @@ package com.iluwatar;
|
|||||||
* Singleton pattern ensures that the class (IvoryTower) can have only one
|
* Singleton pattern ensures that the class (IvoryTower) can have only one
|
||||||
* existing instance and provides global access to that instance.
|
* 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 {
|
public class App {
|
||||||
|
|
||||||
@ -22,5 +24,9 @@ public class App {
|
|||||||
System.out.println("threadSafeIvoryTower1=" + threadSafeIvoryTower1);
|
System.out.println("threadSafeIvoryTower1=" + threadSafeIvoryTower1);
|
||||||
System.out.println("threadSafeIvoryTower2=" + threadSafeIvoryTower2);
|
System.out.println("threadSafeIvoryTower2=" + threadSafeIvoryTower2);
|
||||||
|
|
||||||
|
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
|
||||||
|
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
|
||||||
|
System.out.println("enumIvoryTower1=" + enumIvoryTower1);
|
||||||
|
System.out.println("enumIvoryTower2=" + enumIvoryTower2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
singleton/src/main/java/com/iluwatar/EnumIvoryTower.java
Normal file
15
singleton/src/main/java/com/iluwatar/EnumIvoryTower.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Enum Singleton class.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public enum EnumIvoryTower {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getDeclaringClass().getCanonicalName() + "@" + hashCode();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user