黑馬程式員_類比JDK事件處理機制(觀察者模式)

來源:互聯網
上載者:User

------- android培訓、java培訓、期待與您交流!
----------

以前學AWT事件處理的時候雖然書上竭力解釋實現過程,但還是不明白為什麼Button會調用監聽器的方法,只是記住了這一知識點。昨天聽了馬士兵老師講的觀察者模式才明白了其內部原理。有時候學Java就是這樣,如果不知道它是怎麼實現的,光記住了,總是記不牢,一旦知道了它的實現原理,一下就明白了,用的時候也很順手。所以經常用的JDK類庫,如果可以的話,還是瞭解其實現原理比較好。

下面是我寫的類比AWT事件處理第一版的代碼。

package c21DesignPattern;import java.util.ArrayList;import java.util.List;/** * 功能:類比JDK中AWT架構的實踐處理機制,不類比圖形介面,在測試類別中直接調用button.press(),表示事件產生。 * 關鍵:採用觀察者模式,事件產生者通知事件處理者,而不是事件處理者監聽事件產生者。 * @author renyajun * */public class AWT {public static void main(String[] args) {//建立按鈕,事件監聽器,添加事件監聽器,啟用事件Button btn=new Button();ActionEventListener al=new Button1ActionListener();btn.addActionEventListener(al);btn.press();}}//Button類比AWT中的Button,關鍵點:1.press方法,表示Button被按下,2.list<AtionEventListener>,表示所有Button//被按下事件的處理者。class Button{List<ActionEventListener> actionEventListeners=new ArrayList<ActionEventListener>();public void press(){for(ActionEventListener l:actionEventListeners){l.acionPerformed();}}public void addActionEventListener(ActionEventListener al) {actionEventListeners.add(al);}}interface ActionEventListener{void acionPerformed();}class Button1ActionListener implements ActionEventListener{@Overridepublic void acionPerformed() {System.out.println("文字框的內容變為XXX。");}}

第二版增加了事件類別,使得用戶端可以通過事件參數擷取事件發生的時間和時間源,代碼如下:

package c21DesignPattern;import java.util.ArrayList;import java.util.List;/** * 功能:類比JDK中AWT架構的實踐處理機制,不類比圖形介面,在測試類別中直接調用button.press(),表示事件產生。 * 關鍵:採用觀察者模式,事件產生者通知事件處理者,而不是事件處理者監聽事件產生者。 * @author renyajun * */public class AWT {public static void main(String[] args) {//建立按鈕,事件監聽器,添加事件監聽器,啟用事件Button btn=new Button("類比按鈕");ActionEventListener al=new Button1ActionListener();btn.addActionEventListener(al);btn.press();}}//Button類比AWT中的Button,關鍵點:1.press方法,表示Button被按下,2.list<AtionEventListener>,表示所有Button//被按下事件的處理者。class Button{List<ActionEventListener> actionEventListeners=new ArrayList<ActionEventListener>();private String name;public Button(String name){this.name=name;}public void press(){ActionEvent e=new ActionEvent(System.currentTimeMillis(),this);for(ActionEventListener l:actionEventListeners){l.acionPerformed(e);}}public void addActionEventListener(ActionEventListener al) {actionEventListeners.add(al);}@Overridepublic String toString(){return name;}}interface ActionEventListener{void acionPerformed(ActionEvent e);}class Button1ActionListener implements ActionEventListener{@Overridepublic void acionPerformed(ActionEvent e) {System.out.println("文字框的內容變為XXX。");System.out.println("事件發生的時間是:"+e.getTime()+",事件來源是:"+e.getSource());}}class ActionEvent{private long time;private Object source;public ActionEvent(long time,Object source){this.time=time;this.source=source;}public long getTime() {return time;}public Object getSource() {return source;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.