This article describes the listener mode of Java design pattern. Share it for your reference, as follows:
The listener mode has three elements - event source, event object, and listener.
Event source: As the name implies, the source of the event, such as the button clicked, belongs to the object being listened to;
Event object: This is often confused with the event source. It is often used to package the event source. Remember, it is an event after all, such as clicking events. The difference between the event source is different from the event source. There is no way to feel it;
Listener: This is the core of the listener mode, which defines the actions after an event occurs. Usually, the event object is entered as a parameter as a function defined in the listener.
Here is a simple chestnut:
The story background is that Xiao Ming is an unhygienic child. His mother is very worried about his health and stipulates that her hands must be washed before meals.
Define a naughty child. Naughty children are the objects to be monitored, the source of events, and all events are sent out by the source of events, which seems to be nonsense.
public class Child { private String name; private RemindListener remindListener; public Child(String name){ this.name = name; } public void eat() { if(null!=remindListener){ remindListener.remind(new RemindWashingHandsEvent(this)); } System.out.println("Child eat..."); } public void addListener(RemindListener listener){ remindListener = listener; }}Next is to look at the event object, which, as mentioned above, wraps the event source. Here we define a pre-meal hand washing event.
public class RemindWashingHandsEvent { private Child child; public RemindWashingHandsEvent(Child child){ this.child = child; }} The event object defines the properties and status of the event.
Immediately after the event occurs, the listener's actions are reminded to wash hands.
public class RemindListener { public void remind(RemindWashingHandsEvent remindWashingHandsEvent){ System.out.println("listen to mom, washing hands before eating..."); }}Note that the listener mainly encapsulates actions, that's all.
The above code is just to illustrate the listener mode principle, and the code is simple and not very elegant.
The following inherits or implements the Java standard library, and writes a pair of code casually. It's late at night, and I'll explain it if I have time.
public class Kid{ private String name; private List<Listener> liteners; public Kid(String name) { this.name = name; this.liteners = Lists.newArrayList(); } public void eat(){ for(Listener listener:liteners){ if(listener instance of WashingHandsListener){ WashingHandsListener washingHandsListener = (WashingHandsListener) listener; washingHandsListener.fireAfterEventInvoked(new WashingHandsEvent(this,"wash your hands")); } } System.out.println("Eat..."); } public void addListener(Listener listener){ liteners.add(listener); }}public class Event extends EventObject { /** * Constructs a prototype Event. * * @param source The object on which the Event initially occurred. * @throws IllegalArgumentException if source is null. */ public Event(Object source) { super(source); }}public class WashingHandsEvent extends Event{ private String eventName; /** * Constructs a prototype Event. * * @param source The object on which the Event initially occurred. * @throws IllegalArgumentException if source is null. */ public WashingHandsEvent(Object source,String eventName) { super(source); this.eventName = eventName; } public String getEventName() { return eventName; } public void setEventName(String eventName) { this.eventName = eventName; }}public interface Listener extends java.util.EventListener{ public void fireAfterEventInvoked(Event event);}public class WashingHandsListener implements Listener{ @Override public void fireAfterEventInvoked(Event event) { WashingHandsEvent washingHandsEvent = (WashingHandsEvent) event; System.out.println("Preparation before meals" + washingHandsEvent.getEventName()); }}public class Test { public static void main(String[] args) { Kid xiaoming = new Kid("xiaoming"); xiaoming.addListener(new WashingHandsListener()); xiaoming.eat(); }}Output result:
For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.