Java Listener Example

Source: Internet
Author: User

The listener principle is the observer pattern. Just like a star (event source) gathers a group of fans (the observer). When a star is doing something, it is reported through fans. Subscription information, calculator buttons are the application of this principle.

A small example of a listener is written below:


Package listener;
Import Java.util.EventObject;
/**
* Defines the event object that is used to mark the current action. Must inherit EventObject.
* @author Zyj
*
*/
public class Stateevent extends EventObject {


Private static final long serialversionuid = 5323292975415079206L;

Private enum<actionenum> action;

Public Stateevent (Object source, enum<actionenum> action) {
Super (source);
This.action = action;
}


Public enum<actionenum> getaction () {
Return action;
}


public void Setaction (enum<actionenum> action) {
This.action = action;
}

}


Package listener;
public enum Actionenum {
Wake,sleep;
}


Package listener;
Import Java.util.EventListener;
/**
* Defines the listening interface, which is responsible for monitoring stateevent events. Must inherit EventListener.
* @author Zyj
*
*/
Public interface Statelistener extends EventListener {
void Handlestate (Stateevent event);
}



Package listener;
public class Wakelistener implements Statelistener {

@Override
public void Handlestate (Stateevent event) {
if (event.getaction () = null && event.getaction (). Equals (Actionenum.wake)) {
SYSTEM.OUT.PRINTLN ("You wake up! ");
}
}

}


Package listener;
public class Sleeplistener implements Statelistener {

@Override
public void Handlestate (Stateevent event) {
if (event.getaction () = null && event.getaction (). Equals (Actionenum.sleep)) {
System.out.println ("You slept! ");
}
}

}



Package listener;
Import java.util.ArrayList;
Import java.util.List;

public class Statemanager {
Private list<statelistener> List = new arraylist<statelistener> ();

public boolean AddListener (Statelistener listener) {
if (listener = = NULL)
return false;
return List.add (listener);
}

public boolean RemoveListener (Statelistener listener) {
if (listener = = NULL)
return false;
return List.remove (listener);
}

private void Notifyalllisteners (Stateevent event) {
for (Statelistener listener:list) {
Listener.handlestate (event);
}
}

public void Wake () {
Stateevent event = new Stateevent (this, actionenum.wake);
Notifyalllisteners (event);
}

public void sleep () {
Stateevent event = new Stateevent (this, actionenum.sleep);
Notifyalllisteners (event);
}

}



Package listener;
public class Listenertest {
public static void Main (string[] args) {
Statemanager manager = new Statemanager ();
Manager.addlistener (New Wakelistener ());
Manager.addlistener (New Sleeplistener ());
Manager.wake ();
System.out.println ("-----------------");
Manager.sleep ();
}
}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Listener Example

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.