Java Event mechanism

Source: Internet
Author: User

The Java event mechanism typically consists of three parts: Eventobject,eventlistener and source.

EventObject

Java.util.EventObject is the base class for the event state object, which encapsulates the event source object and information related to the event. All Java event classes need to inherit the class.

EventListener

Java.util.EventListener is a markup interface, meaning that there is no method within the interface. All event listeners need to implement this interface. The event listener registers on the event source, invoking the callback method within the corresponding listener when the property or state of the event source changes.

Source

An event source does not need to implement or inherit any interface or class, which is where the event first occurs. Because the event source needs to register the event listener, there is a need to have a corresponding container for the event listener in the event source.

Here is a simple implementation of the event mechanism that invokes the callback method of the corresponding listener when the state of the event source changes:

Event Object

1 ImportJava.util.EventObject;2 3 /**4  * 5  * @authorthief6  *7  */8  Public classMyEventextendsEventObject {9 Ten     Private Static Final LongSerialversionuid = 1L; One     Private intsourcestate; A      -      PublicMyEvent (Object source) { -         Super(source); theSourcestate =( source) Source). Getflag (); -     } -      -      Public intgetsourcestate () { +         returnsourcestate; -     } +  A}
View Code

Event Listener

1 ImportJava.util.EventListener;2 3 /**4  * 5  * @authorthief6  *7  */8  Public classStatechangelistenerImplementsEventListener {9 Ten      Public voidhandleevent (MyEvent event) { OneSYSTEM.OUT.PRINTLN ("Trigger state Change Event ... "); ASystem.out.println ("Current event source status is:" +event.getsourcestate ()); -System.out.println ("....... ........") "); -     } the}
View Code
1 ImportJava.util.EventListener;2 3 /**4  * 5  * @authorthief6  *7  */8  Public classStatechangetoonelistenerImplementsEventListener {9 Ten      Public voidhandleevent (MyEvent event) { OneSystem.out.println ("Event with trigger status changed to 1 ... "); ASystem.out.println ("Current event source status is:" +event.getsourcestate ()); -System.out.println ("....... ........") "); -     } the      -}
View Code

Event Source

1 ImportJava.util.EventListener;2 ImportJava.util.HashSet;3 ImportJava.util.Set;4 5 /**6  * 7  * @authorthief8  *9  */Ten  Public classSource { One  A     Private intFlag = 0; -set<eventlistener> listeners =NewHashset<eventlistener>(); -  the     /** - * Register Event listeners -      *  -      * @paramListener +      */ -      Public voidAddstatechangelistener (Statechangelistener listener) { + Listeners.add (listener); A     } at      -     /** - * Register Event listeners -      *  -      * @paramListener -      */ in      Public voidAddstatechangetoonelistener (Statechangetoonelistener listener) { - Listeners.add (listener); to     } +  -     /** the * When an event occurs, notify all events that are registered on the event source to respond accordingly *      */ $      Public voidNotifylistener () {Panax Notoginseng          for(EventListener listener:listeners) { -             Try { the((Statechangelistener) listener). Handleevent (NewMyEvent ( This)); +}Catch(Exception e) { A                 if(Flag = = 1) { the((Statechangetoonelistener) listener). Handleevent (NewMyEvent ( This)); +                 } -             } $         } $     } -  -     /** the * Change Status -      */Wuyi      Public voidChangeflag () { theFlag = (Flag = = 0? 1:0); - Notifylistener (); Wu     } -  About      Public intGetflag () { $         returnFlag; -     } -}
View Code

Test class

1  Public classTest {2     3      Public Static voidMain (string[] args) {4         5SOURCE Source =NewSource ();6Source.addstatechangelistener (NewStatechangelistener ());7Source.addstatechangetoonelistener (NewStatechangetoonelistener ());8         9 Source.changeflag ();TenSystem.out.println ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); One Source.changeflag (); ASystem.out.println ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - Source.changeflag (); -     } the  -}

Reference:

http://ericliu1986.iteye.com/blog/629562

Java Event mechanism

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.