Created
September 7, 2014 02:43
-
-
Save KodeSeeker/c342915cacc9b984a08d to your computer and use it in GitHub Desktop.
SimpleSingleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Singleton | |
{ | |
private Singleton singleton; | |
//make the constructor protected- to defeat instantiation. | |
protected Singleton(){} | |
public static Singleton getInstance() { | |
if(singleton == null) { | |
singleton = new ClassicSingleton(); | |
} | |
return singleton; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not thread-safe though. Once you have public static method/property, there's always danger of thread-safety. You need to guard your null check/construction on the singleton member instance. you can do something like this: