| 
 public 
class SimpleSingleton { 
  // ... 
  public 
  static 
  final 
  SimpleSingleton ref = new SimpleSingleton(); 
  private 
  SimpleSingleton() { 
    // ... 
  } 
} 
 | 
    
| 
 public 
class SimpleSingleton2 { 
  // ... 
  private 
  static 
  SimpleSingleton2 ref; 
  private 
  SimpleSingleton2() { 
    // ... 
  } 
  // keine oeffentliches Datenfeld 
  // sondern eine Zugriffsfunktion 
  // --> Erzeugung erst auf Anforderung 
  public 
  static 
  SimpleSingleton2 getRef() { 
    if (ref == null) 
        ref = new SimpleSingleton2(); 
    return 
      ref; 
  } 
} 
 | 
    
| 
 // eine Singleton Klasse, die auch Daten enthaelt 
public 
class SimpleSingleton3 { 
  private 
  Object data; 
  private 
  static 
  SimpleSingleton3 ref; 
  private 
  SimpleSingleton3() { 
    data = null; // oder andere Initialisierung 
  } 
  public 
  static 
  SimpleSingleton3 getRef() { 
    if (ref == null) 
       ref = new SimpleSingleton3(); 
    return 
      ref; 
  } 
  // Zugriffsfunktionen auf die Daten des Singletons 
  public 
  Object getData() { 
    return data; 
  } 
  public 
  void setData(Object data) { 
    this.data = data; 
  } 
} 
 | 
    
| Letzte Änderung: 13.04.2012 | © Prof. Dr. Uwe Schmidt |