Monday, January 23, 2012

The Singleton pattern

Problem: Several clients need to refer to the same thing.
Solution: The Singleton pattern guarantees a class has only one instance.
// Enum singleton - the preferred approach: concise + serializable
public enum Singleton { // implicitly final
    INSTANCE; // implicitly public static final
    public void doSomething() { ... }
}