事件驱动之JDK观察者模式
JDK中關(guān)于觀察者模式主要了解倆個概念
代碼:
定義一個觀察者:
public class EmailObserver implements Observer {@Overridepublic void update(Observable o, Object arg) {if (o instanceof PaymentStatusObservable) {System.out.println("更新訂單事件源");}System.out.println("郵件服務(wù)搜到通知..." + arg);}}定義一個事件源
/** *此類表示模型視圖范例中的 observable 對象,或者說“數(shù)據(jù)”??蓪⑵渥宇惢?#xff0c;表示應用程序想要觀察的對象。一個 observable 對象可以有一個或多個觀察者。觀察者可以是實現(xiàn)了 Observer 接口的任意對象。一個 observable 實例改變后,調(diào)用 Observable 的 notifyObservers 方法的應用程序會通過調(diào)用觀察者的 update 方法來通知觀察者該實例發(fā)生了改變。未指定發(fā)送通知的順序。Observable 類中所提供的默認實現(xiàn)將按照其注冊的重要性順序來通知 Observers,但是子類可能改變此順序,從而使用非固定順序在單獨的線程上發(fā)送通知,或者也可能保證其子類遵從其所選擇的順序。注意,此通知機制與線程無關(guān),并且與 Object 類的 wait 和 notify 機制完全獨立。新創(chuàng)建一個 observable 對象時,其觀察者集是空的。當且僅當 equals 方法為兩個觀察者返回 true 時,才認為它們是相同的。<P>注意點: Observable實現(xiàn)了大部分的邏輯,沒有很好地進行抽象,靈活性降低了存在2個潛在的問題:一個剛剛加入的觀察者錯過了通知;一個剛剛刪除的觀察者被錯誤的通知Observable實現(xiàn)的方法采用synchronized,操作同一個方法時串行,可能會存在效率問題*/ public class PaymentStatusObservable extends Observable {public void updatePaymentStatus(int status) {System.out.println("更新支付狀態(tài)為:" + status);this.setChanged();/*** 如果 hasChanged 方法指示對象已改變,則通知其所有觀察者,并調(diào)用 clearChanged 方法來指示此對象不再改變。每個觀察者都有其 update 方法,其調(diào)用參數(shù)有兩個:observable 對象和 null。換句話說,此方法等效于:notifyObservers(null)*/this.notifyObservers();?public void notifyObservers(Object arg) {
? ? ? ? /*
?? ? ? ? * a temporary array buffer, used as a snapshot of the state of
?? ? ? ? * current Observers.
?? ? ? ? */
? ? ? ? Object[] arrLocal;
?
? ? ? ? synchronized (this) {
? ? ? ? ? ? /* We don't want the Observer doing callbacks into
?? ? ? ? ? ? * arbitrary code while holding its own Monitor.
?? ? ? ? ? ? * The code where we extract each Observable from
?? ? ? ? ? ? * the Vector and store the state of the Observer
?? ? ? ? ? ? * needs synchronization, but notifying observers
?? ? ? ? ? ? * does not (should not).? The worst result of any
?? ? ? ? ? ? * potential race-condition here is that:
?? ? ? ? ? ? * 1) a newly-added Observer will miss a
?? ? ? ? ? ? * ? notification in progress
?? ? ? ? ? ? * 2) a recently unregistered Observer will be
?? ? ? ? ? ? * ? wrongly notified when it doesn't care
?? ? ? ? ? ? */
? ? ? ? ? ? if (!changed)
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? arrLocal = obs.toArray();//得到事先加入的vector的觀察者
? ? ? ? ? ? clearChanged();
? ? ? ? }
?
? ? ? ? for (int i = arrLocal.length-1; i>=0; i--)
? ? ? ? ? ? ((Observer)arrLocal[i]).update(this, arg);//更新觀察者的update方法
? ? }
} }demo:
public class ClientDemo {public static void main(String[] args) {// 被觀察者。即事件源PaymentStatusObservable paymentStatusObservable = new PaymentStatusObservable();// 如果觀察者與集合中已有的觀察者不同,則向?qū)ο蟮挠^察者集中添加此觀察者paymentStatusObservable.addObserver(new EmailObserver());/**
*源碼解析
? public synchronized void addObserver(Observer o) {
? ? ? ? if (o == null)
? ? ? ? ? ? throw new NullPointerException();
? ? ? ? if (!obs.contains(o)) {
? ? ? ? ? ? obs.addElement(o);
? ? ? ? }
? ? }
*/paymentStatusObservable.updatePaymentStatus(1);} }
outPut:
更新支付狀態(tài)為:1
更新訂單事件源
郵件服務(wù)搜到通知...null
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhangfengshi/p/9397519.html
總結(jié)
以上是生活随笔為你收集整理的事件驱动之JDK观察者模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python】Elasticsearc
- 下一篇: centos压缩和解压缩