監聽模式
當事件源對象上發生操作時,將會調用事件監聽器的一個方法,並在調用該方法時把事件對象傳遞過去。
監聽器模式中的3個角色:
- 事件源:具體的事件源,注冊特定的監聽,才可以對事件進行響應。
- 事件對象:封裝了事件源對象以及事件相關的信息,是在事件源和事件監聽器之間傳遞信息的角色。
- 事件監聽器:監聽事件,並進行事件處理或者轉發,必須注冊在事件源上。
一句話,事件源產生事件,事件帶有事件源,監聽器監聽事件。
JDK中當然有現成的事件模型類
監聽器模式和觀察者模式大同小異,但要比觀察者模式復雜一些。一些邏輯需要手動實現,比如注冊監聽器,刪除監聽器,獲取監聽器數量等等,這里的eventObject也是你自己實現的。
先看看JDK中的監聽器類圖
角色之三:事件監聽器
package java.util; /** * A tagging interface that all event listener interfaces must extend. * @since JDK1.1 */ public interface EventListener { }
簡單到不能再簡單了,對吧,甚至連一個聲明的方法都沒有,那它存在的意義在哪?還記得面向對象中的上溯造型嗎,所以它的意義就在於告訴所有的調用者,我是一個監聽器。
角色之二&一:事件對象及事件對象里的事件源
再來看看事件,即Event或EventObject結尾的那個類,里面含有getSource方法,返回的就是事件源,
package java.util; /** * <p> * The root class from which all event state objects shall be derived. * <p> * All Events are constructed with a reference to the object, the "source", * that is logically deemed to be the object upon which the Event in question * initially occurred upon. * * @since JDK1.1 */ public class EventObject implements java.io.Serializable { private static final long serialVersionUID = 5516075349620653480L; /** * The object on which the Event initially occurred. */ protected transient Object source; /** * Constructs a prototypical Event. * * @param source The object on which the Event initially occurred. * @exception IllegalArgumentException if source is null. */ public EventObject(Object source) { if (source == null) throw new IllegalArgumentException("null source"); this.source = source; } /** * The object on which the Event initially occurred. * * @return The object on which the Event initially occurred. */ public Object getSource() { return source; } /** * Returns a String representation of this EventObject. * * @return A a String representation of this EventObject. */ public String toString() { return getClass().getName() + "[source=" + source + "]"; } }
這個類也很簡單,如果說觀察者模式中的上層類和結果還帶了不少邏輯不少方法的話,那么事件驅動模型中的上層類和接口簡直看不到任何東西。沒錯,
事件驅動模型中,JDK的設計者們進行了最高級的抽象,就是讓上層類只是代表了:我是一個事件(含有事件源),或,我是一個監聽者!
示例:
package com.dxz.listener2; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * 事件源. */ public class EventSourceObject { private String name; // 監聽器容器 private Set<CusEventListener> listener; public EventSourceObject() { this.listener = new HashSet<CusEventListener>(); this.name = "defaultname"; } // 給事件源注冊監聽器 public void addCusListener(CusEventListener cel) { this.listener.add(cel); } // 當事件發生時,通知注冊在該事件源上的所有監聽器做出相應的反應(調用回調方法) protected void notifies() { CusEventListener cel = null; Iterator<CusEventListener> iterator = this.listener.iterator(); while (iterator.hasNext()) { cel = iterator.next(); cel.fireCusEvent(new CusEvent(this)); } } public String getName() { return name; } /** * 模擬事件觸發器,當成員變量name的值發生變化時,觸發事件。 * @param name */ public void setName(String name) { if (!this.name.equals(name)) { this.name = name; notifies(); } } } package com.dxz.listener2; import java.util.EventListener; /** * 事件監聽器,實現java.util.EventListener接口。定義回調方法,將你想要做的事放到這個方法下,因為事件源發生相應的事件時會調用這個方法。 */ public class CusEventListener implements EventListener { // 事件發生后的回調方法 public void fireCusEvent(CusEvent e) { EventSourceObject eObject = (EventSourceObject) e.getSource(); System.out.println("My name has been changed!"); System.out.println("I got a new name,named \"" + eObject.getName() + "\""); } } package com.dxz.listener2; import java.util.EventObject; /** * 事件類,用於封裝事件源及一些與事件相關的參數. */ public class CusEvent extends EventObject { private static final long serialVersionUID = 1L; private Object source;// 事件源 public CusEvent(Object source) { super(source); this.source = source; } public Object getSource() { return source; } public void setSource(Object source) { this.source = source; } } package com.dxz.listener2; public class MainTest { public static void main(String[] args) { EventSourceObject object = new EventSourceObject(); // 注冊監聽器 object.addCusListener(new CusEventListener() { @Override public void fireCusEvent(CusEvent e) { super.fireCusEvent(e); } }); // 觸發事件 object.setName("AiLu"); } }
結果:
My name has been changed! I got a new name,named "AiLu"
1. 事件
事件一般繼承自java.util.EventObject類,封裝了事件源對象及跟事件相關的信息。
2. 事件源
事件源是事件發生的地方,由於事件源的某項屬性或狀態發生了改變(比如BUTTON被單擊、TEXTBOX的值發生改變等等)導致某項事件發生。換句話說就是生成了相應的事件對象。因為事件監聽器要注冊在事件源上,所以事件源類中應該要有盛裝監聽器的容器(List、Set等等)。
3. 事件監聽器
事件監聽器實現java.util.EventListener接口,注冊在事件源上,當事件源的屬性或狀態改變時,取得相應的監聽器調用其內部的回調方法。
事件、事件源、監聽器三者之間的聯系
事件源-----產生----->事件------>被事件監聽器發現------>進入事件處理代碼