Skip to content

Instantly share code, notes, and snippets.

@mibrahimdev
Last active January 28, 2017 20:38
Show Gist options
  • Save mibrahimdev/7eb978f016b0ceeb3e235d1fd882349e to your computer and use it in GitHub Desktop.
Save mibrahimdev/7eb978f016b0ceeb3e235d1fd882349e to your computer and use it in GitHub Desktop.
5 design patterns every Android developer should know - Singleton pattern
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