The principle and implementation of Java Listener

Source: Internet
Author: User

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?
  1. /*
  2. * Event Source: Where the event occurred
  3. */
  4. Public class EventSource {
  5. private Ieventlistener Meventlistener;
  6. //Register listener
  7. public void Seteventlistener (Ieventlistener arg) {
  8. Meventlistener = arg;
  9. }
  10. //Trigger event
  11. public void eventhappened () {
  12. Meventlistener.onclickbutton ();
  13. }
  14. }
[Java]View PlainCopyprint?
    1. /*
    2. * Event Listener, event handler
    3. */
    4. Public interface Ieventlistener {
    5. void OnClickButton ();
    6. }
[Java]View PlainCopyprint?
  1. Public class Test {
  2. public static void Main (string[] args) {
  3. //Event source (the object being monitored)
  4. EventSource m1 = new EventSource ();
  5. //Listener
  6. Ieventlistener Meventlistener = new Ieventlistener () {
  7. @Override
  8. public void OnClickButton () {
  9. //TODO auto-generated method stub
  10. System.out.println ("You clicked the button");
  11. }
  12. };
  13. //Register listener to Event source
  14. M1.seteventlistener (Meventlistener);
  15. M1. Eventhappened ();
  16. }
  17. }

"Experimental Results"
You clicked the button.

Second, the complete model of the demo

[Java]View PlainCopyprint?
    1. /*
    2. * Event
    3. */
    4. Public interface IEvent {
    5. void Seteventlistener (Ieventlistener arg);
    6. boolean Clickbutton ();
    7. boolean movemouse ();
    8. }
[Java]View PlainCopyprint?
    1. /*
    2. * Event listener, invoking event handler
    3. */
    4. Public interface Ieventlistener {
    5. void Doevent (IEvent arg);
    6. }
[Java]View PlainCopyprint?
  1. /*
  2. * Event Source: Where the event occurred
  3. */
  4. Public class EventSource implements ievent{
  5. private Ieventlistener Meventlistener;
  6. Boolean button;
  7. Boolean mouse;
  8. //Register listener
  9. @Override
  10. public void Seteventlistener (Ieventlistener arg) {
  11. Meventlistener = arg;
  12. }
  13. //Trigger event
  14. public void mouseeventhappened () {
  15. mouse = true;
  16. Meventlistener.doevent (this);
  17. }
  18. @Override
  19. Public Boolean Clickbutton () {
  20. return button;
  21. //TODO auto-generated method stub
  22. }
  23. @Override
  24. Public Boolean movemouse () {
  25. //TODO auto-generated method stub
  26. return mouse;
  27. }
  28. }
[Java]View PlainCopyprint?
  1. Public class EventSource2 implements IEvent {
  2. private Ieventlistener ml;
  3. Boolean button;
  4. Boolean mouse;
  5. @Override
  6. public void Seteventlistener (Ieventlistener arg) {
  7. ml = arg;
  8. }
  9. @Override
  10. Public Boolean Clickbutton () {
  11. //TODO auto-generated method stub
  12. return button;
  13. }
  14. @Override
  15. Public Boolean movemouse () {
  16. //TODO auto-generated method stub
  17. return mouse;
  18. }
  19. //Trigger event
  20. public void buttoneventhappened () {
  21. button = true;
  22. Ml.doevent (this);
  23. }
  24. }
[Java]View PlainCopyprint?
  1. Public class Test {
  2. public static void Main (string[] args) {
  3. //Event source (the object being monitored)
  4. EventSource m1 = new EventSource ();
  5. EventSource2 m2 = new EventSource2 ();
  6. //Listener
  7. Ieventlistener Meventlistener = new Ieventlistener () {
  8. @Override
  9. public void Doevent (IEvent arg) {
  10. if (true = = arg. Clickbutton ()) {
  11. System.out.println ("You clicked the button");
  12. }Else if (true = = arg. Movemouse ()) {
  13. System.out.println ("You moved the mouse");
  14. }
  15. }
  16. };
  17. //Register listener to Event source
  18. M1.seteventlistener (Meventlistener);
  19. M1.mouseeventhappened ();
  20. //Register listener to Event source
  21. M2.seteventlistener (Meventlistener);
  22. M2.buttoneventhappened ();
  23. }
  24. }

"Experimental Results"
You moved the mouse
You clicked the button.

The principle and implementation of Java Listener

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.