参考链接: http://blog.csdn.net/tianjf0514/article/details/7475164/
参考链接:https://my.oschina.net/bairrfhoinn/blog/177173?p={{currentPage+1}}
在Java中通过Observable类和Observer接口实现了观察者模式。一个Observer对象监视着一个Observable对象的变化,当Observable对象发生变化时,Observer得到通知,就可以进行相应的工作。
如果画面A是显示数据库里面的数据,而画面B修改了数据库里面的数据,那么这时候画面A就要重新Load。这时候就可以用到观察者模式。
SimpleObservable
SimpleObserverpackage com.eastcom.first.spark.data.observer; import java.util.Observable; public class SimpleObservable extends Observable { private int data = 0; public int getData() { return data; } public void setData(int i) { if (this.data != i) { this.data = i; setChanged(); // 只有在setChange()被调用后,notifyObservers()才会去调用update(),否则什么都不干。 notifyObservers(); } } }
package com.eastcom.first.spark.data.observer; import java.util.Observable; import java.util.Observer; public class SimpleObserver implements Observer { public SimpleObserver(SimpleObservable simpleObservable) { simpleObservable.addObserver(this); } @Override public void update(Observable o, Object data) { // TODO Auto-generated method stub System.out.println("Data has changed to" + ((SimpleObservable) o).getData()); } }
SimpleTest
package com.eastcom.first.spark.data.observer; public class SimpleTest { public static void main(String[] args) { SimpleObservable doc = new SimpleObservable(); SimpleObserver view1 = new SimpleObserver(doc); SimpleObserver view2 = new SimpleObserver(doc); SimpleObserver view3 = new SimpleObserver(doc); doc.setData(1); doc.setData(2); doc.setData(2); doc.setData(3); doc.deleteObserver(view2); System.out.println("==================="); doc.setData(1); doc.setData(2); doc.setData(2); doc.setData(3); } }
运行结果
Data has changed to1 Data has changed to1 Data has changed to1 Data has changed to2 Data has changed to2 Data has changed to2 Data has changed to3 Data has changed to3 Data has changed to3 =================== Data has changed to1 Data has changed to1 Data has changed to2 Data has changed to2 Data has changed to3 Data has changed to3
对应一个类,既是观察者同时也是被观察者。具体实例如下:
ObserverA
package com.eastcom.first.spark.data.observer; import java.util.Observable; import java.util.Observer; public class ObserverA extends Observable implements Observer { @Override public void update(Observable o, Object arg) { // TODO Auto-generated method stub ObserverB observerB = (ObserverB) o; System.out.println("observerB changed, the new value of observerB.data is " + observerB.data); this.setChanged(); this.notifyObservers(); } }
ObserverB
package com.eastcom.first.spark.data.observer; import java.util.Observable; import java.util.Observer; public class ObserverB extends Observable implements Observer { int data = 0; @Override public void update(Observable object, Object arg) { System.out.println("ObserverB found that ObserverA changed..."); } public void setData(int data) { this.data = data; this.setChanged(); this.notifyObservers(); } }
ObserverClient
package com.eastcom.first.spark.data.observer; public class ObserverClient { public static void main(String[] args) { ObserverA a = new ObserverA(); ObserverB b = new ObserverB(); a.addObserver(b); b.addObserver(a); b.setData(2); } }
运行结果:
observerB changed, the new value of observerB.data is 2 ObserverB found that ObserverA changed...