gjunhd
奇数观察者OddObserver.java
package com.zj.observers;
import java.util.Observable;
import java.util.Observer;
public class OddObserver implements Observer {
public void update(Observable o, Object arg) {
if (arg == NumsObservable.ODD) {
NumsObservable myObserable = (NumsObservable) o;
System.out.println("OddObserver:Data has changed to " + myObserable.getData());
}
}
}
偶数观察者EvenObserver.java
package com.zj.observers;
import java.util.Observable;
import java.util.Observer;
public class EvenObserver implements Observer {
public void update(Observable o, Object arg) {
if (arg == NumsObservable.EVEN) {
NumsObservable myObserable = (NumsObservable) o;
System.out.println("EvenObserver:Data has changed to " + myObserable.getData());
}
}
}
测试类MultiTest.java
package com.zj.observers;
public class MultiTest {
public static void main(String[] args) {
NumsObservable number = new NumsObservable();
number.addObserver(new OddObserver());
number.addObserver(new EvenObserver());
number.setData(1);
number.setData(2);
number.setData(3);
}
}
结果:
OddObserver:Data has changed to 1
EvenObserver:Data has changed to 2
OddObserver:Data has changed to 3
本文出自 “子 孑” 博客,请务必保留此出处 http://zhangjunhd.blog.51cto.com/113473/68949 |