设计模式-监听器模式-java

说明

应用场景

  • 操作控件:点击按钮、输入框敲击按键、点击鼠标、移动鼠标

  • 监听值变化

三要素

监听器模式有三个要素:事件源、事件对象、监听器。

  • 监听器:事件发生后要执行的操作,一般是接口

  • 事件对象:封装事件相关的信息,比如:点击按钮,创建事件对象,封装事件发生的时间、事件类型(点击、双击)、按钮坐标等

  • 事件源:事件发生的源头,比如:按钮

执行过程

触发事件时,调用 事件源 的方法,在该方法中创建 事件对象,调用事件监听器

三者关系

事件源 与 监听器 的关系

监听器 通常是 事件源 的成员变量

事件源 可以有 一个或多个 监听器,如:单击按钮事件监听器、双击按钮事件监听器等

事件源 提供 添加 监听器 的方法

事件对象 与 监听器 的关系

监听器执行时,事件对象作为参数传入

事件源 与 事件对象 的关系

触发事件时,调用 事件源 的方法,在该方法中创建 事件对象

例子

监听器

一般是接口,只有一个方法,形参是 事件对象

  1. package test;
  2. public interface ButtonListener {
  3. public void handle(ButtonEvent e);
  4. }

事件对象

  1. package test;
  2. import java.util.Date;
  3. public class ButtonEvent {
  4. public static final String TYPE_CLICK="click";
  5. public static final String TYPE_DOUBLE_CLICK="double_click";
  6. /**
  7. * 事件源
  8. */
  9. private Object source;
  10. /**
  11. * 触发事件的时间
  12. */
  13. private Date date;
  14. /**
  15. * 事件类型
  16. */
  17. private String type;
  18. /**
  19. * 事件发生时,事件源的位置(游戏中判断是否被击中等)
  20. */
  21. private int x;
  22. private int y;
  23. /**
  24. *
  25. * @param button 事件源
  26. * @param type 事件类型:单击 或 双击
  27. */
  28. public ButtonEvent(MyButton button,String type) {
  29. this.date = new Date();
  30. this.source = button;
  31. this.type = type;
  32. }
  33. public Object getSource() {
  34. return source;
  35. }
  36. public Date getDate() {
  37. return date;
  38. }
  39. public String getType() {
  40. return type;
  41. }
  42. public int getX() {
  43. return x;
  44. }
  45. public void setX(int x) {
  46. this.x = x;
  47. }
  48. public int getY() {
  49. return y;
  50. }
  51. public void setY(int y) {
  52. this.y = y;
  53. }
  54. }

事件源

  1. package test;
  2. public class MyButton {
  3. /**
  4. * 点击事件监听器
  5. */
  6. private ButtonListener clickListener;
  7. /**
  8. * 按钮上的文字
  9. */
  10. private String text;
  11. public MyButton(String text) {
  12. this.text = text;
  13. }
  14. public String getText() {
  15. return text;
  16. }
  17. public void setText(String text) {
  18. this.text = text;
  19. }
  20. public void setClickListener(ButtonListener clickListener) {
  21. this.clickListener = clickListener;
  22. }
  23. public void click(){
  24. if(clickListener !=null) {
  25. ButtonEvent event = new ButtonEvent(this, ButtonEvent.TYPE_CLICK);
  26. event.setX(10);
  27. event.setY(100);
  28. clickListener.handle(event);
  29. }
  30. }
  31. }

主启动类

  1. package test;
  2. public class Main {
  3. public static void main(String[] args) {
  4. MyButton btn = new MyButton("登录");
  5. btn.setClickListener(new ButtonListener() {
  6. @Override
  7. public void handle(ButtonEvent e) {
  8. MyButton src = (MyButton)e.getSource();
  9. System.out.println("事件源:"+src);
  10. System.out.println("按钮文字:"+src.getText());
  11. System.out.println("事件时间:"+e.getDate());
  12. System.out.println("事件类型:"+e.getType());
  13. System.out.println("位置:"+e.getX()+","+e.getY());
  14. }
  15. });
  16. // 代码触发点击事件,模拟鼠标点击按钮
  17. btn.click();
  18. }
  19. }

原文出处:http://malaoshi.top/show_1GWE6KVnYUo.html