java監聽器的原理與實現

監聽器模型涉及如下三個對象,模型圖以下:java

(1)事件:用戶對組件的一個操做,稱之爲一個事件
(2)事件源:發生事件的組件就是事件源
(3)事件監聽器(處理器):監聽並負責處理事件的方法
app

執行順序以下:ide

一、給事件源註冊監聽器
二、組件接受外部做用,也就是事件被觸發
三、組件產生一個相應的事件對象,並把此對象傳遞給與之關聯的事件處理器
四、事件處理器啓動,並執行相關的代碼來處理該事件。this

 

        監聽器模式:事件源註冊監聽器以後,當事件源觸發事件,監聽器就能夠回調事件對象的方法;更形象地說,監聽者模式是基於:註冊-回調的事件/消息通知處理模式,就是被監控者將消息通知給全部監控者。 spa

一、註冊監聽器:事件源.setListener;
二、回調:事件源實現onListener。.net

 

下面,來看兩個demo。code

1、簡化了上圖所示的模型,僅僅包含事件源與監聽器對象

 

[java] view plain copy
 
print?
  1. /* 
  2.  * 事件源:事件發生的地點 
  3.  */  
  4. public class EventSource {  
  5.     private IEventListener mEventListener;  
  6.   
  7.     // 註冊監聽器  
  8.     public void setEventListener(IEventListener arg) {  
  9.         mEventListener = arg;  
  10.     }  
  11.   
  12.     // 觸發事件  
  13.     public void EventHappened() {  
  14.         mEventListener.onclickButton();  
  15.     }  
  16.   
  17. }  
 
[java] view plain copy
 
print?
  1. /* 
  2.  * 事件監聽器,事件處理器 
  3.  */  
  4. public interface IEventListener {  
  5.     void onclickButton();  
  6. }  
 
[java] view plain copy
 
print?
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.   
  4.         // 事件源(被監聽的對象)  
  5.         EventSource m1 = new EventSource();  
  6.   
  7.         // 監聽器  
  8.         IEventListener mEventListener = new IEventListener() {  
  9.   
  10.             @Override  
  11.             public void onclickButton() {  
  12.                 // TODO Auto-generated method stub  
  13.                 System.out.println("你點擊了按鈕");  
  14.             }  
  15.         };  
  16.   
  17.         // 註冊監聽器到事件源  
  18.         m1.setEventListener(mEventListener);  
  19.         m1.EventHappened();  
  20.     }  
  21. }  

【實驗結果】
你點擊了按鈕

2、完整模型的demoblog

 

 

[java] view plain copy
 
print?
  1. /* 
  2.  * 事件 
  3.  */  
  4. public interface IEvent {  
  5.       
  6.     void setEventListener(IEventListener arg);  
  7.       
  8.     boolean ClickButton();  
  9.       
  10.     boolean MoveMouse();  
  11.   
  12. }  
 
[java] view plain copy
 
print?
  1. /* 
  2.  * 事件監聽器,調用事件處理器 
  3.  */  
  4. public interface IEventListener {  
  5.   
  6.     void doEvent(IEvent arg);  
  7. }  
 
[java] view plain copy
 
print?
  1. /* 
  2.  * 事件源:事件發生的地點 
  3.  */  
  4. public class EventSource implements IEvent{  
  5.     private IEventListener mEventListener;  
  6.     boolean button;  
  7.     boolean mouse;  
  8.       
  9.     //註冊監聽器  
  10.     @Override  
  11.     public void setEventListener(IEventListener arg){  
  12.         mEventListener = arg;  
  13.     }  
  14.       
  15.     //觸發事件  
  16.     public void mouseEventHappened(){  
  17.         mouse = true;  
  18.         mEventListener.doEvent(this);  
  19.     }  
  20.   
  21.     @Override  
  22.     public boolean ClickButton() {  
  23.         return button;  
  24.         // TODO Auto-generated method stub  
  25.           
  26.     }  
  27.   
  28.     @Override  
  29.     public boolean MoveMouse() {  
  30.         // TODO Auto-generated method stub  
  31.         return mouse;  
  32.     }  
  33.   
  34. }  
 
[java] view plain copy
 
print?
  1. public class EventSource2 implements IEvent {  
  2.     private IEventListener ml;  
  3.     boolean button;  
  4.     boolean mouse;  
  5.   
  6.     @Override  
  7.     public void setEventListener(IEventListener arg) {  
  8.         ml = arg;  
  9.     }  
  10.   
  11.     @Override  
  12.     public boolean ClickButton() {  
  13.         // TODO Auto-generated method stub  
  14.         return button;  
  15.     }  
  16.   
  17.     @Override  
  18.     public boolean MoveMouse() {  
  19.         // TODO Auto-generated method stub  
  20.         return mouse;  
  21.     }  
  22.   
  23.     // 觸發事件  
  24.     public void buttonEventHappened() {  
  25.         button = true;  
  26.         ml.doEvent(this);  
  27.     }  
  28.   
  29. }  
 
[java] view plain copy
 
print?
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.   
  4.         // 事件源(被監聽的對象)  
  5.         EventSource m1 = new EventSource();  
  6.         EventSource2 m2 = new EventSource2();  
  7.         // 監聽器  
  8.         IEventListener mEventListener = new IEventListener() {  
  9.   
  10.             @Override  
  11.             public void doEvent(IEvent arg) {  
  12.                 if (true == arg.ClickButton()) {  
  13.                     System.out.println("你點擊了按鈕");  
  14.                 }else if(true == arg.MoveMouse()){  
  15.                     System.out.println("你移動了鼠標");  
  16.                 }  
  17.             }  
  18.         };  
  19.   
  20.         // 註冊監聽器到事件源  
  21.         m1.setEventListener(mEventListener);  
  22.         m1.mouseEventHappened();  
  23.           
  24.         // 註冊監聽器到事件源  
  25.         m2.setEventListener(mEventListener);  
  26.         m2.buttonEventHappened();  
  27.     }  
  28. }  
 

【實驗結果】
你移動了鼠標
你點擊了按鈕

事件

相關文章
相關標籤/搜索