Java事件监听机制与观察者设计模式
一. Java事件监听机制
1. 事件监听三要素: 事件源,事件对象,事件监听器
2. 三要素之间的关系:事件源注册事件监听器后,当事件源上发生某个动作时,事件源就会调用事件监听的一个方法,并将事件对象传递进去,开发者可以利用事件对象操作事件源。
3. 入门程序
1 /** 2 * Created by IntelliJ IDEA. 3 * 4 * @Auther: ShaoHsiung 5 * @Date: 2018/8/29 21:25 6 * @Title: 事件监听入门程序 7 * @Description: 8 */ 9 public class Demo1 { 10 public static void main(String[] args) { 11 Frame frame = new Frame(); 12 frame.setSize(400, 500); 13 frame.setVisible(true); 14 // 为frame注册一个事件监听器 15 frame.addWindowListener(new WindowListener() { 16 @Override 17 public void windowOpened(WindowEvent e) { 18 19 } 20 21 @Override 22 public void windowClosing(WindowEvent e) { 23 System.out.println("再会~~~"); 24 // 通过事件对象获取事件源 25 Frame frame = (Frame) e.getSource(); 26 // 关闭窗口 27 frame.dispose(); 28 } 29 30 @Override 31 public void windowClosed(WindowEvent e) { 32 33 } 34 35 @Override 36 public void windowIconified(WindowEvent e) { 37 38 } 39 40 @Override 41 public void windowDeiconified(WindowEvent e) { 42 43 } 44 45 @Override 46 public void windowActivated(WindowEvent e) { 47 48 } 49 50 @Override 51 public void windowDeactivated(WindowEvent e) { 52 53 } 54 }); 55 } 56 }
二. 设计一个能够被其它类监听的类(如何实现监听机制)
其实就是观察者模式
1. 事件源
1 // 事件源 2 class Door { 3 4 private DoorListener doorListener; 5 6 public void addDoorListener(DoorListener doorListener) { 7 this.doorListener = doorListener; 8 } 9 10 public void open() { 11 if (doorListener != null) { 12 doorListener.doOpen(new DoorEvent(this)); 13 } 14 System.out.println("门打开了。。。"); 15 } 16 17 public void close() { 18 if (doorListener != null) { 19 doorListener.doClose(new DoorEvent(this)); 20 } 21 System.out.println("门关上了。。。"); 22 } 23 }
2. 事件监听器
1 // 监听器 2 interface DoorListener { 3 public void doOpen(DoorEvent doorEvent); 4 public void doClose(DoorEvent doorEvent); 5 }
3. 事件对象
1 // 事件对象 2 class DoorEvent { 3 // source 4 private Door door; 5 public DoorEvent() {} 6 public DoorEvent(Door door) { 7 this.door = door; 8 } 9 10 public Door getDoor() { 11 return door; 12 } 13 14 public void setDoor(Door door) { 15 this.door = door; 16 } 17 }
4. 测试
1 public class Demo2 { 2 3 public static void main(String[] args) { 4 // 门 5 Door door = new Door(); 6 // 注册监听器 7 door.addDoorListener(new DoorListener() { 8 @Override 9 public void doOpen(DoorEvent doorEvent) { 10 System.out.println(doorEvent + " bomb~~~"); 11 } 12 13 @Override 14 public void doClose(DoorEvent doorEvent) { 15 System.out.println(doorEvent + " bomb2~~~"); 16 } 17 }); 18 door.open(); 19 door.close(); 20 } 21 }
完整代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Created by IntelliJ IDEA. 3 * 4 * @Auther: ShaoHsiung 5 * @Date: 2018/8/29 21:45 6 * @Title: 观察者设计模式 7 * @Description: 8 */ 9 public class Demo2 { 10 11 public static void main(String[] args) { 12 // 门 13 Door door = new Door(); 14 // 注册监听器 15 door.addDoorListener(new DoorListener() { 16 @Override 17 public void doOpen(DoorEvent doorEvent) { 18 System.out.println(doorEvent + " bomb~~~"); 19 } 20 21 @Override 22 public void doClose(DoorEvent doorEvent) { 23 System.out.println(doorEvent + " bomb2~~~"); 24 } 25 }); 26 door.open(); 27 door.close(); 28 } 29 } 30 31 // 事件源 32 class Door { 33 34 private DoorListener doorListener; 35 36 public void addDoorListener(DoorListener doorListener) { 37 this.doorListener = doorListener; 38 } 39 40 public void open() { 41 if (doorListener != null) { 42 doorListener.doOpen(new DoorEvent(this)); 43 } 44 System.out.println("门打开了。。。"); 45 } 46 47 public void close() { 48 if (doorListener != null) { 49 doorListener.doClose(new DoorEvent(this)); 50 } 51 System.out.println("门关上了。。。"); 52 } 53 } 54 55 // 监听器 56 interface DoorListener { 57 public void doOpen(DoorEvent doorEvent); 58 public void doClose(DoorEvent doorEvent); 59 } 60 61 // 事件对象 62 class DoorEvent { 63 // source 64 private Door door; 65 public DoorEvent() {} 66 public DoorEvent(Door door) { 67 this.door = door; 68 } 69 70 public Door getDoor() { 71 return door; 72 } 73 74 public void setDoor(Door door) { 75 this.door = door; 76 } 77 }