Java Design Pattern listener pattern example details, design pattern listener

Source: Internet
Author: User

Java Design Pattern listener pattern example details, design pattern listener

This article describes the listener mode of Java design mode. We will share this with you for your reference. The details are as follows:

The listener mode has three elements: Event source, event object, and listener.

Event Source: As the name implies, the source of an event, such as the clicked button, belongs to the Monitored object;
Event object: This is often confused with the event source. It is often used to wrap the event source. Remember, it is an event after all, such as click event, which is different from the event source, wood contains chestnuts;
Listener: this is the core of the listener mode. It defines the action after an event occurs. Generally, the event object is used as the function input parameter defined in the listener.

The following is a simple example:

The background of the story is that James is a child who does not speak about hygiene. His mother is worried about his health and requires that he wash his hands before meals.

Define a bear child. The bear child is the Monitored object, the event source, and all events are sent from the event source. This seems to be a 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, let's take a look at the event object. As described above, the event object encapsulates the event source. Here we define a pre-dinner hand washing event.

public class RemindWashingHandsEvent {  private Child child;  public RemindWashingHandsEvent(Child child){    this.child = child;  }}

The event object defines the attributes and status of the event.
The next step is to define the action of the listener after an event occurs. Here, we will remind you to wash your hands.

public class RemindListener {  public void remind(RemindWashingHandsEvent remindWashingHandsEvent){    System.out.println("listen to mom, washing hands before eating...");  }}

Note that the listener encapsulates the action.

The above code is just to illustrate the listener mode principle. The code is plain and not very elegant.

The following inheritance or implementation of the java standard library, with a hand-written pair of code, late at night, there is time to explain.

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 instanceof listener inghandslistener) {Listener inghandslistener listener inghandslistener = (listener inghandslistener) listener; Listener. fireAfterEventInvoked (new WashingHandsEvent (this, "Hand Washing");} System. out. println ("dinner... ");} public void addListener (Listener listener) {liteners. add (listener) ;}} public class Event extends EventObject {/*** Constructs a prototypical 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 prototypical Event. ** @ param source The object on which the Event initially occurred. * @ throws IllegalArgumentException if source is null. */public jsoninghandsevent (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 Listener (Event event);} public class WashingHandsListener implements Listener {@ Override public void Listener (Event event) {inginghandsevent WashingHandsEvent = (washingHandsEvent) event; System. out. println ("prepare before meals" + paiinghandsevent. 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, refer to this topic: java data structure and algorithm tutorial, Java DOM node skills summary, Java file and directory operation skills summary, and Java cache operation skills Summary

I hope this article will help you with java programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.