Last active
January 28, 2017 20:38
-
-
Save mibrahimdev/7eb978f016b0ceeb3e235d1fd882349e to your computer and use it in GitHub Desktop.
5 design patterns every Android developer should know - Singleton pattern
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 { | |
//instance holder | |
private static Singleton mInstance = null; | |
private String mString; | |
//prevent others from instantiating the class with new keyword | |
private Singleton(){ | |
mString = "Hello"; | |
} | |
//public method to get the Singelton instance | |
public static Singleton getInstance(){ | |
//check if it wasn't created before | |
if(mInstance == null) { | |
mInstance = new Singleton(); | |
} | |
return mInstance; | |
} | |
public String getString(){ | |
return this.mString; | |
} | |
public void setString(String value){ | |
mString = value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment