監(jiān)聽器模式有三個(gè)要素——事件源醇滥、事件對(duì)象、監(jiān)聽器超营。
事件源:顧名思義腺办,事件發(fā)生的源頭,比如點(diǎn)擊的按鈕糟描,屬于被監(jiān)聽的對(duì)象;
事件對(duì)象:這個(gè)經(jīng)常和事件源混淆书妻,它經(jīng)常被用來包裝事件源船响,切記,它畢竟是個(gè)事件躲履,比如點(diǎn)擊事件见间,和事件源的區(qū)別自己感受,木有栗子工猜;
監(jiān)聽器:這個(gè)是監(jiān)聽器模式的核心米诉,定義事件發(fā)生后的動(dòng)作,通常事件對(duì)象作為監(jiān)聽器中定義的函數(shù)入?yún)ⅰ?/p>
下面舉個(gè)簡單的栗子:
故事背景是篷帅,小明是個(gè)不講衛(wèi)生的孩子史侣,他媽媽很擔(dān)心他的健康,規(guī)定必須飯前洗手魏身。
定義一個(gè)熊孩子惊橱。熊孩子就是被監(jiān)聽的對(duì)象,是事件源箭昵,一切事件都是事件源發(fā)出税朴,這似乎是句廢話。
public class Child {
private String name;
private RemindListener remindListener;
public Child(String name){
this.name = name;
}
public void eat() {
if(null!=remindListener){
remindListener.remind(new RemindWashingHandsEvent(this));
}
System.out.println("Child eat...");
}
public void addListener(RemindListener listener){
remindListener = listener;
}
}
接下來是看看事件對(duì)象家制,事件對(duì)象正如上面所述正林,包裝了事件源。我們?cè)谶@里定義一個(gè)飯前洗手事件颤殴。
public class RemindWashingHandsEvent {
private Child child;
public RemindWashingHandsEvent(Child child){
this.child = child;
}
}
事件對(duì)象定義了事件的屬性觅廓、狀態(tài)。
緊接著是定義事件發(fā)生后诅病,監(jiān)聽器的動(dòng)作哪亿,在這里是提醒洗手粥烁。
public class RemindListener {
public void remind(RemindWashingHandsEvent remindWashingHandsEvent){
System.out.println("listen to mom, washing hands before eating...");
}
}
注意,監(jiān)聽器主要封裝了動(dòng)作蝇棉,僅此而已讨阻。
以上代碼,只是為了說明監(jiān)聽器模式原理篡殷,代碼通俗钝吮,不太優(yōu)雅。
下面繼承或?qū)崿F(xiàn)java標(biāo)準(zhǔn)庫板辽,又隨手寫了一對(duì)代碼奇瘦,夜深了,有時(shí)間再解釋劲弦。
public class Kid{
private String name;
private List<Listener> liteners;
public Kid(String name) {
this.name = name;
this.liteners = Lists.newArrayList();
}
public void eat(){
for(Listener listener:liteners){
if(listener instanceof WashingHandsListener){
WashingHandsListener washingHandsListener = (WashingHandsListener) listener;
washingHandsListener.fireAfterEventInvoked(new WashingHandsEvent(this,"洗手"));
}
}
System.out.println("吃飯...");
}
public void addListener(Listener listener){
liteners.add(listener);
}
}
public class Event extends EventObject {
/**
* Constructs a prototypical Event.
*
* @param source The object on which the Event initially occurred.
* @throws IllegalArgumentException if source is null.
*/
public Event(Object source) {
super(source);
}
}
public class WashingHandsEvent extends Event{
private String eventName;
/**
* Constructs a prototypical Event.
*
* @param source The object on which the Event initially occurred.
* @throws IllegalArgumentException if source is null.
*/
public WashingHandsEvent(Object source,String eventName) {
super(source);
this.eventName = eventName;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
}
public interface Listener extends java.util.EventListener{
public void fireAfterEventInvoked(Event event);
}
public class WashingHandsListener implements Listener{
@Override
public void fireAfterEventInvoked(Event event) {
WashingHandsEvent washingHandsEvent = (WashingHandsEvent) event;
System.out.println("飯前準(zhǔn)備"+ washingHandsEvent.getEventName());
}
}
public class Test {
public static void main(String[] args) {
Kid xiaoming = new Kid("xiaoming");
xiaoming.addListener(new WashingHandsListener());
xiaoming.eat();
}
}
輸出結(jié)果: