java监听器原理理解与实现

来源:互联网 发布:mac免费office软件 编辑:程序博客网 时间:2024/05/17 07:42

        监听器模型涉及以下三个对象,模型图如下:

(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. }  
/* * 事件源:事件发生的地点 */public class EventSource {    private IEventListener mEventListener;    // 注册监听器    public void setEventListener(IEventListener arg) {        mEventListener = arg;    }    // 触发事件    public void EventHappened() {        mEventListener.onclickButton();    }}
[java] view plain copy
print?
  1. /* 
  2.  * 事件监听器,事件处理器 
  3.  */  
  4. public interface IEventListener {  
  5.     void onclickButton();  
  6. }  
/* * 事件监听器,事件处理器 */public interface IEventListener {    void onclickButton();}
[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. }  
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();    }}
【实验结果】
你点击了按钮

二、完整模型的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. }  
/* * 事件 */public interface IEvent {    void setEventListener(IEventListener arg);    boolean ClickButton();    boolean MoveMouse();}
[java] view plain copy
print?
  1. /* 
  2.  * 事件监听器,调用事件处理器 
  3.  */  
  4. public interface IEventListener {  
  5.   
  6.     void doEvent(IEvent arg);  
  7. }  
/* * 事件监听器,调用事件处理器 */public interface IEventListener {    void doEvent(IEvent arg);}
[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. }  
/* * 事件源:事件发生的地点 */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;    }}
[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. }  
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);    }}
[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. }  
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();    }}
【实验结果】
你移动了鼠标
你点击了按钮




原创粉丝点击