Java Observer pattern 实例 , 用于 monitor

import java.util.Observable;


public class EventSource extends Observable {


private int value = 0;


public void add(int add) {
value += add;
setChanged();
notifyObservers(add + "!!!" + value);
}


}




























public class MyApp {
public static void main(String[] args) {


final EventSource eventSource = new EventSource();


final ResponseHandler responseHandler = new ResponseHandler();


eventSource.addObserver(responseHandler);


eventSource.add(111);
}
}
























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


public class ResponseHandler implements Observer {


@Override
public void update(Observable arg0, Object arg1) {
System.out.println("???" + arg0);
System.out.println("###" + arg1);
}


}

你可能感兴趣的:(Java Observer pattern 实例 , 用于 monitor)