1. 程式人生 > >java監聽器的原理與實現

java監聽器的原理與實現

來看 class copyto 圖片 http size stat 順序 方法

監聽器模型涉及以下三個對象,模型圖如下:

(1)事件:用戶對組件的一個操作,稱之為一個事件
(2)事件源:發生事件的組件就是事件源
(3)事件監聽器(處理器):監聽並負責處理事件的方法
技術分享圖片

執行順序如下:

1、給事件源註冊監聽器
2、組件接受外部作用,也就是事件被觸發
3、組件產生一個相應的事件對象,並把此對象傳遞給與之關聯的事件處理器
4、事件處理器啟動,並執行相關的代碼來處理該事件。

監聽器模式:事件源註冊監聽器之後,當事件源觸發事件,監聽器就可以回調事件對象的方法;更形象地說,監聽者模式是基於:註冊-回調的事件/消息通知處理模式,就是被監控者將消息通知給所有監控者。

1、註冊監聽器:事件源.setListener;
2、回調:事件源實現onListener。

下面,來看兩個demo。

一、簡化了上圖所示的模型,僅僅包含事件源與監聽器

[java] view plain copy print?
  1. /*
  2. * 事件源:事件發生的地點
  3. */
  4. public class EventSource {
  5. private IEventListener mEventListener;
  6. // 註冊監聽器
  7. public void setEventListener(IEventListener arg) {
  8. mEventListener = arg;
  9. }
  10. // 觸發事件
  11. public void EventHappened() {
  12. mEventListener.onclickButton();
  13. }
  14. }



[java] view plain copy
 
 print?

  1. /*
  2. * 事件監聽器,事件處理器
  3. */
  4. public interface IEventListener {
  5. void onclickButton();
  6. }



[java] view plain copy
 
 print?

  1. public class Test {
  2. public static void main(String[] args) {
  3. // 事件源(被監聽的對象)
  4. EventSource m1 = new EventSource();
  5. // 監聽器
  6. IEventListener mEventListener = new IEventListener() {
  7. @Override
  8. public void onclickButton() {
  9. // TODO Auto-generated method stub
  10. System.out.println("你點擊了按鈕");
  11. }
  12. };
  13. // 註冊監聽器到事件源
  14. m1.setEventListener(mEventListener);
  15. m1.EventHappened();
  16. }
  17. }

【實驗結果】
你點擊了按鈕

二、完整模型的demo

[java] view plain copy print?
  1. /*
  2. * 事件
  3. */
  4. public interface IEvent {
  5. void setEventListener(IEventListener arg);
  6. boolean ClickButton();
  7. boolean MoveMouse();
  8. }



[java] view plain copy
 
 print?

  1. /*
  2. * 事件監聽器,調用事件處理器
  3. */
  4. public interface IEventListener {
  5. void doEvent(IEvent arg);
  6. }



[java] view plain copy
 
 print?

  1. /*
  2. * 事件源:事件發生的地點
  3. */
  4. public class EventSource implements IEvent{
  5. private IEventListener mEventListener;
  6. boolean button;
  7. boolean mouse;
  8. //註冊監聽器
  9. @Override
  10. public void setEventListener(IEventListener arg){
  11. mEventListener = arg;
  12. }
  13. //觸發事件
  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 plain copy
 
 print?

  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. // 觸發事件
  20. public void buttonEventHappened() {
  21. button = true;
  22. ml.doEvent(this);
  23. }
  24. }



[java] view plain copy
 
 print?

  1. public class Test {
  2. public static void main(String[] args) {
  3. // 事件源(被監聽的對象)
  4. EventSource m1 = new EventSource();
  5. EventSource2 m2 = new EventSource2();
  6. // 監聽器
  7. IEventListener mEventListener = new IEventListener() {
  8. @Override
  9. public void doEvent(IEvent arg) {
  10. if (true == arg.ClickButton()) {
  11. System.out.println("你點擊了按鈕");
  12. }else if(true == arg.MoveMouse()){
  13. System.out.println("你移動了鼠標");
  14. }
  15. }
  16. };
  17. // 註冊監聽器到事件源
  18. m1.setEventListener(mEventListener);
  19. m1.mouseEventHappened();
  20. // 註冊監聽器到事件源
  21. m2.setEventListener(mEventListener);
  22. m2.buttonEventHappened();
  23. }
  24. }

【實驗結果】
你移動了鼠標
你點擊了按鈕

java監聽器的原理與實現