監聽器模式有三個要素——事件源、事件對象、監聽器。
事件源:顧名思義,事件發生的源頭,比如點擊的按鈕,屬于被監聽的對象;
事件對象:這個經常和事件源混淆,它經常被用來包裝事件源,切記,它畢竟是個事件,比如點擊事件,和事件源的區別自己感受,木有栗子;
監聽器:這個是監聽器模式的核心,定義事件發生后的動作,通常事件對象作為監聽器中定義的函數入參。
下面舉個簡單的栗子:
故事背景是,小明是個不講衛生的孩子,他媽媽很擔心他的健康,規定必須飯前洗手。
定義一個熊孩子。熊孩子就是被監聽的對象,是事件源,一切事件都是事件源發出,這似乎是句廢話。
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;
}
}
接下來是看看事件對象,事件對象正如上面所述,包裝了事件源。我們在這里定義一個飯前洗手事件。
public class RemindWashingHandsEvent {
private Child child;
public RemindWashingHandsEvent(Child child){
this.child = child;
}
}
事件對象定義了事件的屬性、狀態。
緊接著是定義事件發生后,監聽器的動作,在這里是提醒洗手。
public class RemindListener {
public void remind(RemindWashingHandsEvent remindWashingHandsEvent){
System.out.println("listen to mom, washing hands before eating...");
}
}
注意,監聽器主要封裝了動作,僅此而已。
以上代碼,只是為了說明監聽器模式原理,代碼通俗,不太優雅。
下面繼承或實現java標準庫,又隨手寫了一對代碼,夜深了,有時間再解釋。
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("飯前準備"+ washingHandsEvent.getEventName());
}
}
public class Test {
public static void main(String[] args) {
Kid xiaoming = new Kid("xiaoming");
xiaoming.addListener(new WashingHandsListener());
xiaoming.eat();
}
}
輸出結果:
image