java監聽器的原理與實現


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

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

執行順序如下:

1、給事件源注冊監聽器
2、組件接受外部作用,也就是事件被觸發
3、組件產生一個相應的事件對象,並把此對象傳遞給與之關聯的事件處理器
4、事件處理器啟動,並執行相關的代碼來處理該事件。

 

        監聽器模式:事件源注冊監聽器之后,當事件源觸發事件,監聽器就可以回調事件對象的方法;更形象地說,監聽者模式是基於:注冊-回調的事件/消息通知處理模式,就是被監控者將消息通知給所有監控者。 

1、注冊監聽器:事件源.setListener;
2、回調:事件源實現onListener。

 

下面,來看兩個demo。

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

 

[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. }  

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

二、完整模型的demo

 

 

[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. }  
 
        

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM