Adding initialization on demand holder idiom.

This commit is contained in:
leogtzr 2016-12-24 00:49:46 -07:00
parent c94c8a3e74
commit 20b1c2bd49

View File

@ -27,22 +27,24 @@ package com.iluwatar.singleton;
*/
public final class IvoryTower {
/**
* Static to class instance of the class.
*/
private static final IvoryTower INSTANCE = new IvoryTower();
/**
* Private constructor so nobody can instantiate the class.
*/
private IvoryTower() {}
private static class IvoryTowerHolder {
/**
* Static to class instance of the class.
*/
private static final IvoryTower INSTANCE = new IvoryTower();
}
/**
* To be called by user to obtain instance of the class.
*
* @return instance of the singleton.
*/
public static IvoryTower getInstance() {
return INSTANCE;
return IvoryTowerHolder.INSTANCE;
}
}