class SimpleCounter extends Observable{ public int count = 0; public void increase() { count++; setChanged(); notifyObservers(); } } class SimpleObserver implements Observer { String id; public SimpleObserver( String id ) { this.id = id ; } public void update( Observable sender, Object message ) { System.out.println( "From " + id + " New value " + ((SimpleCounter) sender).count ); } } class ObserveTest { public static void main( String args[] ) { SimpleCounter test = new SimpleCounter(); SimpleObserver a = new SimpleObserver( "a" ); test.addObserver( a); test.addObserver( new SimpleObserver( "b" ) ); test.increase(); test.addObserver( new SimpleObserver( "c" ) ); test.increase(); } }
class ObserveTest { public static void main( String args[] ) { SimpleCounter test = new SimpleCounter(); SimpleObserver a = new SimpleObserver( "a" ); test.addObserver( a); test.addObserver( new SimpleObserver( "b" ) ); test.increase(); test.addObserver( new SimpleObserver( "c" ) ); test.increase(); } }
class ObserveTest { public static void main( String args[] ) { SimpleCounter test = new SimpleCounter(); SimpleObserver a = new SimpleObserver( "a" ); test.addObserver( a); test.addObserver( a ); test.increase(); test.addObserver( a ); test.increase(); } }
class ObserveTest { public static void main( String args[] ) { SimpleCounter test = new SimpleCounter(); SimpleObserver a = new SimpleObserver( "a" ); SimpleObserver b = new SimpleObserver( "b" ); test.addObserver( a); test.addObserver( b); test.increase(); System.out.println( "Delete a" ); test.deleteObserver( a ); test.increase(); System.out.println( "Delete a again" ); test.deleteObserver( a ); test.increase(); System.out.println( "Delete all" ); test.deleteObservers( ); test.increase(); } }