Implementing a Simple Event Notifier : Observer Pattern « Design Pattern « Java






Implementing a Simple Event Notifier

  


import java.util.Observable;
import java.util.Observer;

class MyModel extends Observable {
  public synchronized void setChanged() {
    super.setChanged();
  }
}

public class Main {
  public static void main(String[] argv) throws Exception {
    MyModel model = new MyModel();
    model.addObserver(new Observer() {
      public void update(Observable o, Object arg) {
      }
    });
    model.setChanged();
    Object arg = "new information";
    model.notifyObservers(arg);
  }
}

   
    
  








Related examples in the same category

1.Observable and observer
2.Observer Pattern - Example in Java
3.Observer Pattern in Java 2
4.A simple demo of Observable and Observer
5.Using Observer pattern with two observers observing a changing integer