监听器模式有三个要素——事件源、事件对象、监听器。
事件源:顾名思义,事件发生的源头,比如点击的按钮,属于被监听的对象;
事件对象:这个经常和事件源混淆,它经常被用来包装事件源,切记,它毕竟是个事件,比如点击事件,和事件源的区别自己感受,木有栗子;
监听器:这个是监听器模式的核心,定义事件发生后的动作,通常事件对象作为监听器中定义的函数入参。
下面举个简单的栗子:
故事背景是,小明是个不讲卫生的孩子,他妈妈很担心他的健康,规定必须饭前洗手。
定义一个熊孩子。熊孩子就是被监听的对象,是事件源,一切事件都是事件源发出,这似乎是句废话。
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(); } }
输出结果: