2014-08-11 21:43:08 +03:00
|
|
|
package com.iluwatar;
|
|
|
|
|
2014-08-31 08:42:01 +03:00
|
|
|
/**
|
2014-10-13 22:15:45 -07:00
|
|
|
*
|
2014-10-07 16:23:37 +01:00
|
|
|
* Singleton pattern ensures that the class (IvoryTower) can have only one
|
2015-03-14 16:59:02 +01:00
|
|
|
* existing instance per java classloader instance and provides global access to it.
|
|
|
|
*
|
2014-11-26 09:15:40 +08:00
|
|
|
* http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java
|
|
|
|
*
|
2015-03-14 16:59:02 +01:00
|
|
|
* The risk of this pattern is that bugs resulting from setting a singleton up in a distributed environment can
|
|
|
|
* be tricky to debug, since it will work fine if you debug with a single classloader. Additionally, these
|
|
|
|
* problems can crop up a while after the implementation of a singleton, since they may start out synchronous and
|
|
|
|
* only become async with time, so you it may not be clear why you are seeing certain changes in behaviour.
|
|
|
|
*
|
|
|
|
* http://stackoverflow.com/questions/17721263/singleton-across-jvm-or-application-instance-or-tomcat-instance
|
2014-08-31 08:42:01 +03:00
|
|
|
*/
|
2014-10-07 16:23:37 +01:00
|
|
|
public class App {
|
|
|
|
|
2014-10-08 13:42:12 +01:00
|
|
|
public static void main(String[] args) {
|
2014-10-07 16:23:37 +01:00
|
|
|
|
2014-11-26 22:47:44 +02:00
|
|
|
// eagerly initialized singleton
|
2014-10-08 13:42:12 +01:00
|
|
|
IvoryTower ivoryTower1 = IvoryTower.getInstance();
|
|
|
|
IvoryTower ivoryTower2 = IvoryTower.getInstance();
|
|
|
|
System.out.println("ivoryTower1=" + ivoryTower1);
|
|
|
|
System.out.println("ivoryTower2=" + ivoryTower2);
|
2014-10-07 16:23:37 +01:00
|
|
|
|
2014-11-26 22:47:44 +02:00
|
|
|
// lazily initialized singleton
|
2014-10-13 22:15:45 -07:00
|
|
|
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower1 = ThreadSafeLazyLoadedIvoryTower
|
|
|
|
.getInstance();
|
|
|
|
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower2 = ThreadSafeLazyLoadedIvoryTower
|
|
|
|
.getInstance();
|
|
|
|
System.out.println("threadSafeIvoryTower1=" + threadSafeIvoryTower1);
|
|
|
|
System.out.println("threadSafeIvoryTower2=" + threadSafeIvoryTower2);
|
|
|
|
|
2014-11-26 22:47:44 +02:00
|
|
|
// enum singleton
|
2014-11-25 16:10:17 +08:00
|
|
|
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
|
|
|
|
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
|
2014-11-24 18:01:44 +08:00
|
|
|
System.out.println("enumIvoryTower1=" + enumIvoryTower1);
|
|
|
|
System.out.println("enumIvoryTower2=" + enumIvoryTower2);
|
2014-12-05 00:02:50 +03:30
|
|
|
|
|
|
|
InitializingOnDemandHolderIdiom demandHolderIdiom = InitializingOnDemandHolderIdiom.getInstance();
|
|
|
|
System.out.println(demandHolderIdiom);
|
|
|
|
InitializingOnDemandHolderIdiom demandHolderIdiom2 = InitializingOnDemandHolderIdiom.getInstance();
|
|
|
|
System.out.println(demandHolderIdiom2);
|
|
|
|
|
|
|
|
ThreadSafeDoubleCheckLocking dcl1 = ThreadSafeDoubleCheckLocking.getInstance();
|
|
|
|
System.out.println(dcl1);
|
|
|
|
ThreadSafeDoubleCheckLocking dcl2 = ThreadSafeDoubleCheckLocking.getInstance();
|
|
|
|
System.out.println(dcl2);
|
2014-10-08 13:42:12 +01:00
|
|
|
}
|
2014-08-11 21:43:08 +03:00
|
|
|
}
|