Created
March 14, 2012 05:34
-
-
Save pooooch/2034315 to your computer and use it in GitHub Desktop.
a observer example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pattern; | |
import java.util.Observable; | |
import java.util.Observer; | |
public class ObserverExample { | |
public static void main(String[] args) { | |
ObData ObData = new ObData(); | |
ObData.addObserver(new Observer() { | |
@Override | |
public void update(Observable o, Object arg) { | |
if (o instanceof ObData) { | |
ObData ob = (ObData) o; | |
System.out.println("data is:" + ob.getData()); | |
if (arg != null) { | |
System.out.println(" arg is:" + arg); | |
} | |
} | |
} | |
}); | |
ObData.deliver(2); | |
ObData.setData(10); | |
ObData.deliver(3); | |
} | |
} | |
class ObData extends Observable { | |
private int data = 0; | |
public int getData() { | |
return data; | |
} | |
public void setData(int data) { | |
this.data = data; | |
this.deliver(data); | |
} | |
public void deliver(Object arg) { | |
this.setChanged(); | |
this.notifyObservers(arg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment