監聽器模型涉及以下三個對象,模型圖如下:
(1)事件:用戶對組件的一個操作,稱之為一個事件
(2)事件源:發生事件的組件就是事件源
(3)事件監聽器(處理器):監聽並負責處理事件的方法
執行順序如下:
1、給事件源注冊監聽器
2、組件接受外部作用,也就是事件被觸發
3、組件產生一個相應的事件對象,並把此對象傳遞給與之關聯的事件處理器
4、事件處理器啟動,並執行相關的代碼來處理該事件。
監聽器模式:事件源注冊監聽器之后,當事件源觸發事件,監聽器就可以回調事件對象的方法;更形象地說,監聽者模式是基於:注冊-回調的事件/消息通知處理模式,就是被監控者將消息通知給所有監控者。
1、注冊監聽器:事件源.setListener;
2、回調:事件源實現onListener。
下面,來看兩個demo。
一、簡化了上圖所示的模型,僅僅包含事件源與監聽器
- /*
- * 事件源:事件發生的地點
- */
- public class EventSource {
- private IEventListener mEventListener;
- // 注冊監聽器
- public void setEventListener(IEventListener arg) {
- mEventListener = arg;
- }
- // 觸發事件
- public void EventHappened() {
- mEventListener.onclickButton();
- }
- }
/*
* 事件源:事件發生的地點
*/
public class EventSource {
private IEventListener mEventListener;
// 注冊監聽器
public void setEventListener(IEventListener arg) {
mEventListener = arg;
}
// 觸發事件
public void EventHappened() {
mEventListener.onclickButton();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- /*
- * 事件監聽器,事件處理器
- */
- public interface IEventListener {
- void onclickButton();
- }
/*
* 事件監聽器,事件處理器
*/
public interface IEventListener {
void onclickButton();
}
- 1
- 2
- 3
- 4
- 5
- 6
- public class Test {
- public static void main(String[] args) {
- // 事件源(被監聽的對象)
- EventSource m1 = new EventSource();
- // 監聽器
- IEventListener mEventListener = new IEventListener() {
- @Override
- public void onclickButton() {
- // TODO Auto-generated method stub
- System.out.println(”你點擊了按鈕”);
- }
- };
- // 注冊監聽器到事件源
- m1.setEventListener(mEventListener);
- m1.EventHappened();
- }
- }
public class Test {
public static void main(String[] args) {
// 事件源(被監聽的對象)
EventSource m1 = new EventSource();
// 監聽器
IEventListener mEventListener = new IEventListener() {
@Override
public void onclickButton() {
// TODO Auto-generated method stub
System.out.println("你點擊了按鈕");
}
};
// 注冊監聽器到事件源
m1.setEventListener(mEventListener);
m1.EventHappened();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
【實驗結果】
你點擊了按鈕
二、完整模型的demo
- /*
- * 事件
- */
- public interface IEvent {
- void setEventListener(IEventListener arg);
- boolean ClickButton();
- boolean MoveMouse();
- }
/*
* 事件
*/
public interface IEvent {
void setEventListener(IEventListener arg);
boolean ClickButton();
boolean MoveMouse();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- /*
- * 事件監聽器,調用事件處理器
- */
- public interface IEventListener {
- void doEvent(IEvent arg);
- }
/*
* 事件監聽器,調用事件處理器
*/
public interface IEventListener {
void doEvent(IEvent arg);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- /*
- * 事件源:事件發生的地點
- */
- public class EventSource implements IEvent{
- private IEventListener mEventListener;
- boolean button;
- boolean mouse;
- //注冊監聽器
- @Override
- public void setEventListener(IEventListener arg){
- mEventListener = arg;
- }
- //觸發事件
- public void mouseEventHappened(){
- mouse = true;
- mEventListener.doEvent(this);
- }
- @Override
- public boolean ClickButton() {
- return button;
- // TODO Auto-generated method stub
- }
- @Override
- public boolean MoveMouse() {
- // TODO Auto-generated method stub
- return mouse;
- }
- }
/*
* 事件源:事件發生的地點
*/
public class EventSource implements IEvent{
private IEventListener mEventListener;
boolean button;
boolean mouse;
//注冊監聽器
@Override
public void setEventListener(IEventListener arg){
mEventListener = arg;
}
//觸發事件
public void mouseEventHappened(){
mouse = true;
mEventListener.doEvent(this);
}
@Override
public boolean ClickButton() {
return button;
// TODO Auto-generated method stub
}
@Override
public boolean MoveMouse() {
// TODO Auto-generated method stub
return mouse;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- public class EventSource2 implements IEvent {
- private IEventListener ml;
- boolean button;
- boolean mouse;
- @Override
- public void setEventListener(IEventListener arg) {
- ml = arg;
- }
- @Override
- public boolean ClickButton() {
- // TODO Auto-generated method stub
- return button;
- }
- @Override
- public boolean MoveMouse() {
- // TODO Auto-generated method stub
- return mouse;
- }
- // 觸發事件
- public void buttonEventHappened() {
- button = true;
- ml.doEvent(this);
- }
- }
public class EventSource2 implements IEvent {
private IEventListener ml;
boolean button;
boolean mouse;
@Override
public void setEventListener(IEventListener arg) {
ml = arg;
}
@Override
public boolean ClickButton() {
// TODO Auto-generated method stub
return button;
}
@Override
public boolean MoveMouse() {
// TODO Auto-generated method stub
return mouse;
}
// 觸發事件
public void buttonEventHappened() {
button = true;
ml.doEvent(this);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- public class Test {
- public static void main(String[] args) {
- // 事件源(被監聽的對象)
- EventSource m1 = new EventSource();
- EventSource2 m2 = new EventSource2();
- // 監聽器
- IEventListener mEventListener = new IEventListener() {
- @Override
- public void doEvent(IEvent arg) {
- if (true == arg.ClickButton()) {
- System.out.println(”你點擊了按鈕”);
- }else if(true == arg.MoveMouse()){
- System.out.println(”你移動了鼠標”);
- }
- }
- };
- // 注冊監聽器到事件源
- m1.setEventListener(mEventListener);
- m1.mouseEventHappened();
- // 注冊監聽器到事件源
- m2.setEventListener(mEventListener);
- m2.buttonEventHappened();
- }
- }
public class Test {
public static void main(String[] args) {
// 事件源(被監聽的對象)
EventSource m1 = new EventSource();
EventSource2 m2 = new EventSource2();
// 監聽器
IEventListener mEventListener = new IEventListener() {
@Override
public void doEvent(IEvent arg) {
if (true == arg.ClickButton()) {
System.out.println("你點擊了按鈕");
}else if(true == arg.MoveMouse()){
System.out.println("你移動了鼠標");
}
}
};
// 注冊監聽器到事件源
m1.setEventListener(mEventListener);
m1.mouseEventHappened();
// 注冊監聽器到事件源
m2.setEventListener(mEventListener);
m2.buttonEventHappened();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
【實驗結果】
你移動了鼠標
你點擊了按鈕