java監聽器原理理解與實現


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

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

 

執行順序如下:

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

 

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

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

 

下面,來看兩個demo。

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

 

[java]  view plain  copy
 
  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. }  
/*
 * 事件源:事件發生的地點
 */
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
[java]  view plain  copy
 
  1. /* 
  2.  * 事件監聽器,事件處理器 
  3.  */  
  4. public interface IEventListener {  
  5.     void onclickButton();  
  6. }  
/*
 * 事件監聽器,事件處理器
 */
public interface IEventListener {
    void onclickButton();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
[java]  view plain  copy
 
  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. }  
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

 

 

[java]  view plain  copy
 
  1. /* 
  2.  * 事件 
  3.  */  
  4. public interface IEvent {  
  5.       
  6.     void setEventListener(IEventListener arg);  
  7.       
  8.     boolean ClickButton();  
  9.       
  10.     boolean MoveMouse();  
  11.   
  12. }  
/*
 * 事件
 */
public interface IEvent {

    void setEventListener(IEventListener arg);

    boolean ClickButton();

    boolean MoveMouse();

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
[java]  view plain  copy
 
  1. /* 
  2.  * 事件監聽器,調用事件處理器 
  3.  */  
  4. public interface IEventListener {  
  5.   
  6.     void doEvent(IEvent arg);  
  7. }  
/*
 * 事件監聽器,調用事件處理器
 */
public interface IEventListener {

    void doEvent(IEvent arg);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
[java]  view plain  copy
 
  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. }  
/*
 * 事件源:事件發生的地點
 */
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
[java]  view plain  copy
 
  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. }  
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
[java]  view plain  copy
 
  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. }  
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

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

 


免責聲明!

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



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