Singleton- oder Fabrikklasse

Gespeichert von Erik Wegner am/um Do, 01.04.2010 - 22:27
Body
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

Kategorien