Skip to content

Instantly share code, notes, and snippets.

@mweppler
Created August 3, 2011 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mweppler/1123199 to your computer and use it in GitHub Desktop.
Save mweppler/1123199 to your computer and use it in GitHub Desktop.
Wiki Definition: "In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object."
// Traditional Simple Way:
public class Singleton {
private static final Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return instance;
}
}
// The solution of Bill Pugh:
public class Singleton {
private Singleton() {
}
private static class SingletonHolder {
public static final Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
// The Enum-way:
public enum Singleton {
INSTANCE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment