Java中的事件机制的参与者有3种角色:
1. event object:就是事件产生时具体的“事件”,用于listener的相应的方法之中,作为参数,一般存在于listerner的方法之中
2. event source:具体的接受事件的实体,比如说,你点击一个button,那么button就是eventsource,这样你必须使button对某些事件进行响应,你就需要注册特定的listener,比如说MouseEvent之中的MouseClicked方法,这时它就必须有了add方法
3. event listener:具体的进行监听的事件类,当有其对应的event object产生的时候,它就调用相应的方法,进行处理。在windows程序设计里边这种响应使用callback机制来实现的
import org.junit.jupiter.api.Test; public class MonitorTest { @Test public void monitoringTest() throws Exception { MonitorManager manager=new MonitorManager(); manager.addListener(new CarListener()); manager.addListener(new BicycleListener()); manager.addListener(new PersonListener()); manager.straight(); System.out.println("======================="); manager.turnLeft(); } }
import java.util.EventObject; public class MonitorEvent extends EventObject { /** * 1 直行绿灯 * 2 左拐绿灯 */ private Integer heartRateValue; public MonitorEvent(Object source, Integer heartRateValue) { super(source); this.heartRateValue = heartRateValue; } public Integer getHeartRateValue() { return heartRateValue; } public void setHeartRateValue(Integer heartRateValue) { this.heartRateValue = heartRateValue; } }
import java.util.EventListener; public interface MonitorListener extends EventListener { void monitorEvent(MonitorEvent event); }
import java.util.Collection; import java.util.HashSet; import java.util.Iterator; public class MonitorManager { private Collection listeners; //存储所有注册进来的事件监听器对象; public void addListener(MonitorListener listener) //把一个事件监听器对象注册进来; { if(this.listeners == null) { this.listeners = new HashSet(); } this.listeners.add(listener); } public void removeListener(MonitorListener listener) //移除一个已经注册的事件监听器对象; { if(this.listeners != null) { this.listeners.remove(listener); } } /** * 直行 */ protected void straight() { if(this.listeners != null) { MonitorEvent event = new MonitorEvent(this, 1); notifyListeners(event); } } /** * 左拐 */ protected void turnLeft() //触发开门事件 { if(this.listeners != null) { MonitorEvent event = new MonitorEvent(this, 2); notifyListeners(event); } } private void notifyListeners(MonitorEvent event) { Iterator inter = this.listeners.iterator(); while(inter.hasNext()) { MonitorListener listener = (MonitorListener)inter.next(); listener.monitorEvent(event); } } }
===管理类
import java.util.Collection; import java.util.HashSet; import java.util.Iterator; public class MonitorManager { private Collection listeners; //存储所有注册进来的事件监听器对象; public void addListener(MonitorListener listener) //把一个事件监听器对象注册进来; { if(this.listeners == null) { this.listeners = new HashSet(); } this.listeners.add(listener); } public void removeListener(MonitorListener listener) //移除一个已经注册的事件监听器对象; { if(this.listeners != null) { this.listeners.remove(listener); } } /** * 直行 */ protected void straight() { if(this.listeners != null) { MonitorEvent event = new MonitorEvent(this, 1); notifyListeners(event); } } /** * 左拐 */ protected void turnLeft() { if(this.listeners != null) { MonitorEvent event = new MonitorEvent(this, 2); notifyListeners(event); } } /** * 通知每一个注册上来的监测,去执行 * @param event */ private void notifyListeners(MonitorEvent event) { Iterator inter = this.listeners.iterator(); while(inter.hasNext()) { MonitorListener listener = (MonitorListener)inter.next(); listener.monitorEvent(event); } } }
===三个具体的类
public class CarListener implements MonitorListener { public void monitorEvent(MonitorEvent event) { if (event.getHeartRateValue() == 1) { System.out.println("直行绿灯 汽车 可直行"); } if (event.getHeartRateValue() == 2) { System.out.println("左拐绿灯 汽车 可左拐"); } } }
public class BicycleListener implements MonitorListener { public void monitorEvent(MonitorEvent event) { if (event.getHeartRateValue() == 1) { System.out.println("直行绿灯 自行车 可直行"); } if (event.getHeartRateValue() == 2) { System.out.println("左拐绿灯 自行车 可左拐"); } } }
public class PersonListener implements MonitorListener { public void monitorEvent(MonitorEvent event) { if (event.getHeartRateValue() == 1) { System.out.println("直行绿灯 人 可直行"); } if (event.getHeartRateValue() == 2) { System.out.println("左拐绿灯 人 不可通行"); } } }