Java event handling mechanism
There are 3 roles for participants in event mechanism in java:
1.event object: event state object, used in the corresponding methods of listener, as parameters, generally exists in the listener method.
2.event source: The specific event source. For example, if you click on a button, the button is the event source. If you want the button to respond to certain events, you need to register a specific listener.
3.event listener: Define a clear Java method accordingly for the occurrence of each clear event. These methods are centrally defined in the event listener (EventListener) interface, which must inherit java.util.EventListener. The class that implements some or all methods in the event listener interface is the event listener.
As events occur, the corresponding state is usually encapsulated in the event state object, which must be inherited from java.util.EventObject. The event status object is passed as a single parameter to the listener method that should respond to the event. The identity of the event source that issues a particular event is: defines the registration method for the event listener in accordance with the specified design format and accepts references to the specified event listener interface instance.
For specific listening event class, when it listens to the event object, it calls the corresponding method and processes it.
Let’s take a look at the event package provided by jdk:
public interface EventListener: The tag interface that all event listener interfaces must be extended.
public class EventObject extends Object implements Serializable
All event state objects will derive the root class from which they are. All Events reference the object "source" when constructed, which is logically considered to be the object that initially occurs about the Event.
(1) Create the DoorEvent class through the DoorEvent.java file, which inherits the EventObject.
/*** To define an event object, you must inherit EventObject*/public class DoorEvent extends EventObject { private static final long serialVersionUID = 6496098798146410884L; private String doorState = "";// Indicates the status of the door, there are two types of public DoorEvent(Object source, String doorState) { super(source); this.doorState = doorState; } public void setDoorState(String doorState) { this.doorState = doorState; } public String getDoorState() { return this.doorState; }}(2) Define a new event listening interface, which is inherited from EventListener; this interface contains a handler for doorEvent events:
/*** Define the listening interface, responsible for listening to DoorEvent events*/public interface DoorListener extends EventListener { public void doorEvent(DoorEvent event);}Through the above interface, we define event listening classes, which specifically implement the listening function and event handling function.
/*** This class implements the door 1 listening interface, and performs specific door opening and closing actions*/public class DoorListener1 implements DoorListener { @Override public void doorEvent(DoorEvent event) { // TODO Auto-generated method stub if (event.getDoorState() != null && event.getDoorState().equals("open")) { System.out.println("door1 open"); } else { System.out.println("door1 closed"); } }}/*** This class implements the door 2 listening interface, which performs specific door opening, door closing, and light opening and light closing actions*/public class DoorListener2 implements DoorListener { @Override public void doorEvent(DoorEvent event) { // TODO Auto-generated method stub if (event.getDoorState() != null && event.getDoorState().equals("Open")) { System.out.println("Door2 opens, turning on the light in the corridor at the same time"); } else { System.out.println("Door2 closes, turning on the light in the corridor at the same time"); } }}(3) Create an event source class through DoorManager.java, which uses a Collection listeners object to store all event listener objects. The storage method is through addDoorListener(..). notifyListeners(..) is a method that triggers an event, which is used to notify the system: the event has occurred, and you can call the corresponding processing function.
/*** Event source object, here you can imagine it as a remote control that controls the opening and closing of the door, * (If it is in swing, it is similar to a button)*/public class DoorManager { private Collection listeners; /** * Add event* * @param listener * DoorListener */ public void addDoorListener(DoorListener listener) { if (listeners == null) { listeners = new HashSet(); } listeners.add(listener); } /** * Remove event* * @param listener * DoorListener */ public void removeDoorListener(DoorListener listener) { if (listeners == null) return; listeners.remove(listener); } /** * Trigger the door opening event*/ protected void fireWorkspaceOpened() { if (listeners == null) return; DoorEvent event = new DoorEvent(this, "open"); notifyListeners(event); } /** * Trigger the door closing event*/ protected void fireWorkspaceClosed() { if (listeners == null) return; DoorEvent event = new DoorEvent(this, "close"); notifyListeners(event); } /** * Notify all DoorListeners */ private void notifyListeners(DoorEvent event) { Iterator iter = listeners.iterator(); while (iter.hasNext()) { DoorListener listener = (DoorListener) iter.next(); listener.doorEvent(event); } }}(4) Okay, finally write a test program to test our customized events. This program should not be difficult to understand:)
/*** The main program is imagined as who wants to open the door*/public class DoorMain { public static void main(String[] args) { DoorManager manager = new DoorManager(); manager.addDoorListener(new DoorListener1());// Add listener to door 1 manager.addDoorListener(new DoorListener2());// Add listener to door 2 // Open door manager.fireWorkspaceOpened(); System.out.println("I've come in"); // Close manager.fireWorkspaceClosed(); }}Run DoorMain
Door 1 opens Door 2 opens, while opening the lights in the corridor
I've come in
Door 1 closes door 2 closes, while closing the corridor lights
Thank you for reading, I hope it can help you. Thank you for your support for this site!