My Java note----event mechanism

Source: Internet
Author: User
Tags event listener

# Java Events (Java event)
# Java.util.eventobject,java.util.eventlistener
# Victor
# 2016.06.16

Java Event handling mechanism

The participants in the event mechanism in Java have 3 roles:
1. Event object: an instance of a class of events.
As events occur, the corresponding states are usually encapsulated in the event state object, which must inherit from Java.util.EventObject. The event state object is passed as a single parameter to the listener method that should respond to the event.
2. Event Source: The producer of the specific event sources, events.
The identity of the event source that emits a particular event is a conforming design format that defines the registration method for the event listener and accepts a reference to the specified event listener interface instance.
3. Event Listener: Events Listener, the consumer of the event.
Specific to the Listener event class, when it hears the event object is generated, it calls the appropriate method to process.

<Note>
Public interface EventListener: All event listener interfaces must be extended by the markup interface.
public class EventObject extends Object implements Serializable. All event state objects are derived from the root class. All events, when constructed, refer to the object "source", which logically considers the object to be the object that originated the event.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++

Operation Result:

Console>>Console is started.lights>>lights is changing. Light0>>is offlight1>>is offlight2>>is onlight3>>is offlight4>>is onlight5>>is onlight6>>is offlight7>>is onlight8>>is onlight9>>is onlights>>lights is changing. Light0>>is onlight1>>is onlight2>>is onlight3>>is onlight4>>is offlight5>>is onlight6>>is onlight7>>is offlight8>>is onlight9>> is off
OUTPUT

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++

JAVA CODE:

1. Defining Event Objects

 Packageevent;ImportJava.util.EventObject; Public classLighteventextendsEventObject {Private Static Final LongSerialversionuid = -1256654879784096588l; Private BooleanLightstate =false; PrivateString Lightname;  PublicString Getlightname () {returnLightname; }     Public Booleangetlightstate () {returnlightstate; }     PublicLightevent (Object source,string name,BooleanState ) {        Super(source);  This. lightstate =State ;  This. Lightname =name; }}
Event Class

2. Define the event listener interface and the specific listener class

 Packageevent;ImportJava.util.EventListener; Public InterfaceLightlistenerextendsEventListener { Public Abstract voidListenlight (Lightevent event);} Packageevent; Public classLightlistener1Implementslightlistener{ Public voidListenlight (Lightevent event) {if(Event.getlightstate ()) {System.out.println ("" "+event.getlightname () +" >> is on "); }        Else{System.out.println ("" "+event.getlightname () +" >> is Off "); }    };}
Listener Interface & Concrete Listener

3. Defining event sources and registering classes

//Register Packageevent;ImportJava.util.*; Public classListenerregister { PublicList<eventlistener> ListenerList =NewArraylist<eventlistener>();  Public voidAddListener (EventListener listener) {Listenerlist.add (listener); }     Public voidRemoveListener (EventListener listener) {listenerlist.remove (listener); }         Public voidNotifylisteners (Lightevent event) { for(EventListener tmplistener:listenerlist) {((Lightlistener) tmplistener). Listenlight (event); }    }}//EventSource Packageevent;ImportJava.util.*; Public classConsole {PrivateListenerregister Register =NewListenerregister (); Private Boolean[] Lights =New Boolean[10] ; PrivateRandom Randomer =NewRandom ();  Public voidSetlights (BooleanLightintindex) {         This. lights[index] =Light ; String name= "Light" +index; Lightevent Event=NewLightevent ( This, Name,light);    Notifylisteners (event); }         Public voidNotifylisteners (Lightevent event) {register.notifylisteners (event); }     Public voidAddListener (EventListener listener) {Register.listenerlist.add (listener); }     Public voidRemoveListener (EventListener listener) {register.listenerlist.remove (listener); }         Public Static voidMain (string[] args)throwsinterruptedexception {//TODO auto-generated Method StubSystem.out.println ("Console>> Console is started."); Console Console=NewConsole (); Console.addlistener (Newlightlistener1 ());  while(true) {System.out.println ("Lights>> lights is changing.");  for(inti=0;i<10;i++) {console.setlights (Console.randomer.nextBoolean (), i); } thread.sleep (1000); }    }}
EventSource & Register

My Java note----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.