The listener model involves the following three objects, the model diagram is as follows:
(1) Event: A user's action on a component, called an event
(2) Event Source: The component in which the event occurred is the source of the event
(3) Event Listener (processor): A method for monitoring and handling events
The execution sequence is as follows:
1. Register Listener for event source
2, the component accepts the external action, namely the event is triggered
3. The component produces a corresponding event object and passes this object to the event handler associated with it
4. The event handler starts and executes the relevant code to handle the event.
Listener mode: After the event source registers the listener, when the event source triggers the event, the listener can callback the event object's method, and more visually, the listener pattern is based on: Registration-callback event/message notification processing mode, that is, the monitored person notifies all monitors.
1, Register Listener: Event source. Setlistener;
2, Callback: Event source implementation Onlistener.
Let's take a look at two demos.
First, simplified the model shown, only contains the event source and listener
[Java]View PlainCopyprint?
- /*
- * Event Source: Where the event occurred
- */
- Public class EventSource {
- private Ieventlistener Meventlistener;
- //Register listener
- public void Seteventlistener (Ieventlistener arg) {
- Meventlistener = arg;
- }
- //Trigger event
- public void eventhappened () {
- Meventlistener.onclickbutton ();
- }
- }
[Java]View PlainCopyprint?
- /*
- * Event Listener, event handler
- */
- Public interface Ieventlistener {
- void OnClickButton ();
- }
[Java]View PlainCopyprint?
- Public class Test {
- public static void Main (string[] args) {
- //Event source (the object being monitored)
- EventSource m1 = new EventSource ();
- //Listener
- Ieventlistener Meventlistener = new Ieventlistener () {
- @Override
- public void OnClickButton () {
- //TODO auto-generated method stub
- System.out.println ("You clicked the button");
- }
- };
- //Register listener to Event source
- M1.seteventlistener (Meventlistener);
- M1. Eventhappened ();
- }
- }
"Experimental Results"
You clicked the button.
Second, the complete model of the demo
[Java]View PlainCopyprint?
- /*
- * Event
- */
- Public interface IEvent {
- void Seteventlistener (Ieventlistener arg);
- boolean Clickbutton ();
- boolean movemouse ();
- }
[Java]View PlainCopyprint?
- /*
- * Event listener, invoking event handler
- */
- Public interface Ieventlistener {
- void Doevent (IEvent arg);
- }
[Java]View PlainCopyprint?
- /*
- * Event Source: Where the event occurred
- */
- Public class EventSource implements ievent{
- private Ieventlistener Meventlistener;
- Boolean button;
- Boolean mouse;
- //Register listener
- @Override
- public void Seteventlistener (Ieventlistener arg) {
- Meventlistener = arg;
- }
- //Trigger event
- public void mouseeventhappened () {
- mouse = true;
- Meventlistener.doevent (this);
- }
- @Override
- Public Boolean Clickbutton () {
- return button;
- //TODO auto-generated method stub
- }
- @Override
- Public Boolean movemouse () {
- //TODO auto-generated method stub
- return mouse;
- }
- }
[Java]View PlainCopyprint?
- Public class EventSource2 implements IEvent {
- private Ieventlistener ml;
- Boolean button;
- Boolean mouse;
- @Override
- public void Seteventlistener (Ieventlistener arg) {
- ml = arg;
- }
- @Override
- Public Boolean Clickbutton () {
- //TODO auto-generated method stub
- return button;
- }
- @Override
- Public Boolean movemouse () {
- //TODO auto-generated method stub
- return mouse;
- }
- //Trigger event
- public void buttoneventhappened () {
- button = true;
- Ml.doevent (this);
- }
- }
[Java]View PlainCopyprint?
- Public class Test {
- public static void Main (string[] args) {
- //Event source (the object being monitored)
- EventSource m1 = new EventSource ();
- EventSource2 m2 = new EventSource2 ();
- //Listener
- Ieventlistener Meventlistener = new Ieventlistener () {
- @Override
- public void Doevent (IEvent arg) {
- if (true = = arg. Clickbutton ()) {
- System.out.println ("You clicked the button");
- }Else if (true = = arg. Movemouse ()) {
- System.out.println ("You moved the mouse");
- }
- }
- };
- //Register listener to Event source
- M1.seteventlistener (Meventlistener);
- M1.mouseeventhappened ();
- //Register listener to Event source
- M2.seteventlistener (Meventlistener);
- M2.buttoneventhappened ();
- }
- }
"Experimental Results"
You moved the mouse
You clicked the button.
The principle and implementation of Java Listener