Beobachter: Beispiel (I)

public class Counter extends Observable {
    private int count = 0;
    public void step () { this.count ++;
        this.setChanged(); 
        this.notifyObservers();   }   }
public class Watcher implements Observer {
    private final int threshold;
    public void update(Observable o, Object arg) {
       if (((Counter)o).getCount() >= this.threshold) {
           System.out.println ("alarm");  }   }  }
public static void main(String[] args) {
    Counter c = new Counter (); Watcher w = new Watcher (3);
    c.addObserver(w); c.step(); c.step (); c.step (); }



Johannes Waldmann 2013-06-11