Last active
December 17, 2015 23:58
Java design pattern : Singleton
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 MySingleton { | |
private static MySingleton instance; | |
public String customVar; | |
public MySingleton() | |
{ | |
if (instance == null) | |
{ | |
// Create the instance | |
instance = new MySingleton(); | |
} | |
} | |
public static MySingleton getInstance() | |
{ | |
// Return the instance | |
return instance; | |
} | |
private MySingleton() | |
{ | |
// Constructor hidden because this is a singleton | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment