Java基礎(chǔ)教程之事件和監(jiān)聽器
事件監(jiān)聽器是經(jīng)常可以遇到的一種設(shè)計(jì)模式,一般用在這樣一種場(chǎng)景下:當(dāng)模塊的一部分A在完成后需要通知其他的軟件模塊B,而等待通知的模塊B在事先不需要采用輪詢的方式來查看另一個(gè)模塊A是否通知自己。即,當(dāng)某事件發(fā)生,則監(jiān)聽器立刻就知道了該事件。這種模式大量的應(yīng)用在GUI設(shè)計(jì)中,比如按鈕的點(diǎn)擊,狀態(tài)欄上狀態(tài)的改變等等。
接口的設(shè)計(jì)
我們需要一個(gè)對(duì)事件(event)的抽象,同樣需要一個(gè)對(duì)監(jiān)聽器(listener)的抽象。我們可以把接口抽的很簡(jiǎn)單:
這個(gè)是事件源的接口,只需要提供一個(gè)可以獲取事件類型的方法即可:
- package listenerdemo.framework;
- /**
- * @author juntao.qiu
- */
- public interface EventListener {
- /**
- * handle the event when it raise
- * @param event
- */
- public void handleEvent(EventSource event);
- }
監(jiān)聽器接口,提供一個(gè)當(dāng)事件發(fā)生后的處理方法即可:
- package listenerdemo.framework;
- public interface EventSource {
- public final int EVENT_TIMEOUT = 1;
- public final int EVENT_OVERFLOW = 2;
- /**
- * get an integer to identify a special event
- * @return
- */
- public int getEventType();
- }
實(shí)例化事件
我們舉一個(gè)實(shí)現(xiàn)了事件源接口(EventSource)的類TimeoutEvent 來說明如何使用事件監(jiān)聽器模型:
- package listenerdemo;
- import listenerdemo.framework.*;
- public class TimeOutEvent implements EventSource{
- private int type;
- public TimeOutEvent(){
- this.type = EventSource.EVENT_TIMEOUT;;
- }
- public int getEventType() {
- return this.type;
- }
- }
這個(gè)事件的類型為EVENT_TIMEOUT, 當(dāng)操作超時(shí)時(shí)觸發(fā)該事件,我們假設(shè)這樣一個(gè)場(chǎng)景:一個(gè)定時(shí)器T, 先設(shè)置這個(gè)定時(shí)器的時(shí)間為t,當(dāng)t到時(shí)后,則觸發(fā)一個(gè)超時(shí)事件,當(dāng)然,事件是需要監(jiān)聽器來監(jiān)聽才有意義的。我們看看這個(gè)定時(shí)器的實(shí)現(xiàn):
- package listenerdemo;
- import listenerdemo.framework.*;
- /**
- * @author juntao.qiu
- */
- public class Timer extends Thread{
- private EventListener listener;
- private int sleepSeconds;
- public Timer(int seconds){
- this.sleepSeconds = seconds;
- }
- public void setEventListener(EventListener listener){
- this.listener = listener;
- }
- public void run(){
- for(int i = sleepSeconds;i>0;i--){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException ex) {
- System.err.println(ex.getMessage());
- }
- }
- raiseTimeoutEvent();//raise一個(gè)TimeOut事件給監(jiān)聽器
- }
- private void raiseTimeoutEvent(){
- this.listener.handleEvent(new TimeOutEvent());
- }
- }
使用事件及其監(jiān)聽器
在類Tester的execute()方法中,我們先設(shè)置一個(gè)定時(shí)器,這個(gè)定時(shí)器初始化為3秒,設(shè)置好定時(shí)器后,程序進(jìn)入一個(gè)while(true)循環(huán)中,當(dāng)定時(shí)器到時(shí)后,它會(huì)發(fā)送一個(gè)timeout事件給當(dāng)前線程Tester,此時(shí)我們可以設(shè)置execute中的while條件為false,退出死循環(huán)。流程很清晰了,我們來看看代碼:
- package listenerdemo;
- import listenerdemo.framework.*;
- /**
- * @author juntao.qiu
- */
- public class EventListenerTester implements EventListener{
- private boolean loop = true;
- public void execute(){
- Timer timer = new Timer(3);//初始化一個(gè)定時(shí)器
- timer.setEventListener(this);//設(shè)置本類為監(jiān)聽器
- timer.start();
- while(loop){
- try{
- Thread.sleep(1000);
- System.out.println("still in while(true) loop");
- }catch(Exception e){
- System.err.println(e.getMessage());
- }
- }
- System.out.println("interupted by time out event");
- }
- //實(shí)現(xiàn)了EventListener接口
- public void handleEvent(EventSource event) {
- int eType = event.getEventType();
- switch(eType){//判斷事件類型,我們可以有很多種的事件
- case EventSource.EVENT_TIMEOUT:
- this.loop = false;
- break;
- case EventSource.EVENT_OVERFLOW:
- break;
- default:
- this.loop = true;
- break;
- }
- }
- public static void main(String[] args){
- EventListenerTester tester = new EventListenerTester();
- tester.execute();
- }
- }
運(yùn)行結(jié)果如下:
run:
still in while(true) loop
still in while(true) loop
still in while(true) loop
interupted by time out event
程序正是按照預(yù)期的方式運(yùn)行了,當(dāng)然,為了說明主要問題,我們的事件,對(duì)事件的處理,監(jiān)聽器的接口都盡可能的保持簡(jiǎn)單。如果想要完成更復(fù)雜的功能,可以參考文章中的方法自行擴(kuò)充,但是大概流程文中都已經(jīng)說到。
【編輯推薦】