博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
事件——监听器模式
阅读量:7143 次
发布时间:2019-06-29

本文共 1828 字,大约阅读时间需要 6 分钟。

hot3.png

 想研究 汤姆猫,发现里面很多监听器 : LifecycleEvent,Lifecycle,ServletContextListener(Spring)等,故作此文。

java在gui方面用到了大量该模式,当初上大学时用过swing,awt,现在全忘了。有兴趣的可以深究一下。

事件-监听器模式需要四个类:事件源,事件,监听器(interface),具体的监听器

事件源产生事件,如窗口产生点击、关闭事件。

监听器是具体监听器的抽象,面向接口的编程。事件源不需要知道监听器的具体实现细节,只需调用抽要即可。

jdk已有该模式的规范,

//监听器接口package java.util;public interface EventListener {}//事件package java.util;/** * 

* The root class from which all event state objects shall be derived. *

* All Events are constructed with a reference to the object, the "source", * that is logically deemed to be the object upon which the Event in question * initially occurred upon. * * @since JDK1.1 */public class EventObject implements java.io.Serializable { private static final long serialVersionUID = 5516075349620653480L; /** * The object on which the Event initially occurred. */ protected transient Object source; /** * Constructs a prototypical Event. * * @param source The object on which the Event initially occurred. * @exception IllegalArgumentException if source is null. */ public EventObject(Object source) { if (source == null) throw new IllegalArgumentException("null source"); this.source = source; } /** * The object on which the Event initially occurred. * * @return The object on which the Event initially occurred. */ public Object getSource() { return source; } /** * Returns a String representation of this EventObject. * * @return A a String representation of this EventObject. */ public String toString() { return getClass().getName() + "[source=" + source + "]"; }}

这个监听器是个空接口,我们可以自己定义一个EventList接口,包含一个void onEvent(ConcreateEventObject  eventObject)方法。

本文头部的链接写的很好,读者可以点进去细读。thanks

 

 

转载于:https://my.oschina.net/u/1380557/blog/797710

你可能感兴趣的文章
【题目】英文字符进行频率的统计,直方图输出
查看>>
Ztree手风琴效果(第三版)
查看>>
「坐上时光机,查找编译压缩后的文件最初的样子」gulp-sourcemaps 使用说明
查看>>
java 泛型中 T、E ... 和 问号(通配符)的区别
查看>>
MyEclipse使用总结——MyEclipse去除网上复制下来的来代码带有的行号
查看>>
java service wrapper日志参数设置及优化
查看>>
android build.prop详解
查看>>
Spring aop 切面编程
查看>>
C3P0连接池使用教程
查看>>
数据结构——红黑树
查看>>
高通平台MSM8916LCM模块移植(一)-bootloader部分【转】
查看>>
oracle表空间不足相关问题解决办法
查看>>
CentOS-7 在windows server 2012下的虚拟机安装教程
查看>>
函数调用过程栈帧变化详解
查看>>
Android项目实战(三十二):圆角对话框Dialog
查看>>
Word或Excel里画柱状图和折线图组合体
查看>>
C# CRC16 查表法
查看>>
js中获取键盘事件
查看>>
面试(4)-spring-Spring面试题和答案
查看>>
请教 JTable 里的单元格如何使得双击进入单元格后,单元格的内容处于全选中状态...
查看>>