Thread safe implementations(examples) on Wiki - Singleton Pattern on Wikipedia
As in the link above, a single-element enum
type is the best way to implement a Singleton for any Java that supports enums.
One of the best yet simple ones:
public class ClientFactory{
private ClientFactory() {}
private static ClientFactory INSTANCE=null;
public static ClientFactory getInstance() {
if(INSTANCE==null){
synchronize(ClientFactory.class){
if(INSTANCE==null) // check again within synchronized block to guard for race condition
INSTANCE=new ClientFactory();
}
}
return INSTANCE;
}
}
Source:
Wikipedia