Java‎ > ‎

Observ

Observer/Observable design patter native implementation in Java



package kr5;

import java.util.*;


// This is the observing class.
class Watcher implements Observer {
    public void update(Observable obj, Object arg) {
        System.out.println("update() called, count is " + ((Integer) arg).intValue());
    }
}


//This is the class being observed.
class BeingWatched extends Observable {
    void counter(int period) {
        for (; period >= 0; period--) {
            setChanged();
            notifyObservers(new Integer(period));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println("Sleep interrupted");
            }
        }
    }
}


class ObserverDemo {
    public static void main(String args[]) {
        BeingWatched observed = new BeingWatched();
        Watcher observing = new Watcher();
        /* Add the observing to the list of observers for
        observed object. */
        observed.addObserver(observing);
        observed.counter(10);
    }
}



Comments