Created
August 3, 2011 17:19
-
-
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."
This file contains hidden or 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
// 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