Skip to main content

Singleton- oder Fabrikklasse

Submitted by Erik Wegner on
Body

Code-Muster für ein Singleton in Java

public class SimpleSingleton {
  // Create the single instance, make it available statically, and
  // don't let it be redefined.
  private static final SimpleSingleton instance = new SimpleSingleton();
  // Allow subclasses to override the constructor, if necessary.
  protected SimpleSingleton() {
    // Whatever...
  }
  // Accessor only.
  public static SimpleSingleton getInstance() {
    return instance;
  }
  // Methods on the object to actually do something useful.
  public void doSomething() {
    // Whatever
  }
}

Quelle: http://www.javaworld.com/javaworld/javatips/jw-javatip52.html