觀察者模式-Java自定義事件

          核心提示:觀察者模式-Java自定義事件 [正文]: MyEventTest.java: package wintys.event; import javax.swing.event.EventListenerList; import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; /** *Java的事件機制/自定義事件. 運
          觀察者模式-Java自定義事件?

          MyEventTest.java:
          1. package ?wintys.event;? ??
          2. import ?javax.swing.event.EventListenerList;? ??
          3. import ?java.util.Date;? ??
          4. import ?java.text.DateFormat;? ??
          5. import ?java.text.SimpleDateFormat;? ??
          6. ??
          7. /**? ?
          8. *?Java的事件機制/自定義事件.? ?
          9. 運行結果:? ?
          10. do?something?interesting?in?source?here.? ?
          11. listener?detects?[event]:wintys.event.MyEvent[source=wintys.event.MySource@18158? ?
          12. 59]?[occur?at]:2009-10-11?16:27:49? ?
          13. listener?detects?[event]:wintys.event.MyEvent[source=wintys.event.MySource@18158? ?
          14. 59]?[occur?at]:2009-10-11?16:27:49? ?
          15. *?@version?2009-10-11? ?
          16. *?@author?天堂露珠?(wintys@gmail.com)? ?
          17. *?@see?http://www.aygfsteel.com/wintys? ?
          18. */ ? ??
          19. class ?MyEventTest{? ??
          20. ???? public ? static ? void ?main(String[]?args){? ??
          21. ????????MySource?source?=? new ?MySource();? ??
          22. ????????MyListener?myListener?=? new ?MyListener(){? ??
          23. ???????????? public ? void ?doMyAction(MyEvent?e){? ??
          24. ????????????????System.out.println( "listener?detects?" ?+?e);? ??
          25. ????????????}? ??
          26. ????????};? ??
          27. ????????source.addMyListener(myListener);? ??
          28. ????????source.addMyListener(myListener);? ??
          29. ????????source.addMyListener(myListener);? ??
          30. ????????source.removeMyListener(myListener);? ??
          31. ??
          32. ????????source.doSomething();? ??
          33. ????}? ??
          34. }? ??
          35. ??
          36. /**? ?
          37. *?自定義的事件.? ?
          38. *?@version?2009-10-11? ?
          39. *?@author?天堂露珠(wintys@gmail.com)? ?
          40. *?@see?http://www.aygfsteel.com/wintys? ?
          41. */ ? ??
          42. class ?MyEvent? extends ?java.util.EventObject{? ??
          43. ???? private ?Date?date; //記錄事件發生的時間? ??
          44. ??
          45. ???? public ?MyEvent(Object?source?,?Date?date){? ??
          46. ???????? super (source);? ??
          47. ??
          48. ???????? this .date?=?date;? ??
          49. ????}? ??
          50. ??
          51. ???? public ?String?toString(){? ??
          52. ????????DateFormat?df?=? new ?SimpleDateFormat( "yyyy-MM-dd?HH:mm:ss" );? ??
          53. ????????String?dt?=?df.format(date);? ??
          54. ??
          55. ???????? return ? "[event]:" ?+? super .toString()?+? "?[occur?at]:" ?+?dt;? ??
          56. ????}? ??
          57. }? ??
          58. ??
          59. /**? ?
          60. *?自定義事件監聽器接口.? ?
          61. *?@version?2009-10-11? ?
          62. *?@author?天堂露珠(wintys@gmail.com)? ?
          63. *?@see?http://www.aygfsteel.com/wintys? ?
          64. */ ? ??
          65. interface ?MyListener? extends ?java.util.EventListener{? ??
          66. ???? void ?doMyAction(MyEvent?e);? ??
          67. }? ??
          68. ??
          69. /**? ?
          70. *?自定義事件源.? ?
          71. *?@version?2009-10-11? ?
          72. *?@author?天堂露珠(wintys@gmail.com)? ?
          73. *?@see?http://www.aygfsteel.com/wintys? ?
          74. */ ? ??
          75. class ?MySource{? ??
          76. ???? /**? ?
          77. ?????*?保存注冊的監聽器列表.? ?
          78. ?????*?子類可以使用它保存自己的事件監聽器(非MyListener監聽器)列表.? ?
          79. ?????*/ ? ??
          80. ???? protected ?EventListenerList?listenerList?=? new ?EventListenerList();? ??
          81. ???? private ?MyEvent?myEvent?=? null ; //fireDoMyAction()使用此變量? ??
          82. ??
          83. ???? /**? ?
          84. ?????*?沒有做任何事? ?
          85. ?????*/ ? ??
          86. ???? public ?MySource(){? ??
          87. ????}? ??
          88. ???? /**? ?
          89. ?????*?添加一個MyListener監聽器? ?
          90. ?????*/ ? ??
          91. ???? public ? void ?addMyListener(MyListener?listener){? ??
          92. ????????listenerList.add(MyListener. class ?,?listener);? ??
          93. ????}? ??
          94. ??
          95. ???? /**? ?
          96. ?????*?移除一個已注冊的MyListener監聽器.? ?
          97. ?????*?如果監聽器列表中已有相同的監聽器listener1、listener2,? ?
          98. ?????*?并且listener1==listener2,? ?
          99. ?????*?那么只移除最近注冊的一個監聽器。? ?
          100. ?????*/ ? ??
          101. ???? public ? void ?removeMyListener(MyListener?listener){? ??
          102. ????????listenerList.remove(MyListener. class ?,?listener);? ??
          103. ????}? ??
          104. ??
          105. ???? /**? ?
          106. ?????*?@return?在此對象上監聽的所有MyListener類型的監聽器? ?
          107. ?????*/ ? ??
          108. ???? public ?MyListener[]?getMyListeners(){? ??
          109. ???????? return ?(MyListener[])listenerList.getListeners(MyListener. class );? ??
          110. ????}? ??
          111. ??
          112. ???? //Winty:Copy?directly?from?javax.swing.event.EventListenerList? ??
          113. ???? /*Notify?all?listeners?that?have?registered?interest?for? ?
          114. ???????notification?on?this?event?type.??The?event?instance? ?
          115. ???????is?lazily?created?using?the?parameters?passed?into? ?
          116. ???????the?fire?method.? ?
          117. ?????*/ ? ??
          118. ???? protected ? void ?fireDoMyAction()?{? ??
          119. ????????? //?getListenerList()?Guaranteed?to?return?a?non-null?array? ??
          120. ?????????Object[]?listeners?=?listenerList.getListenerList();? ??
          121. ????????? //?Process?the?listeners?last?to?first,?notifying? ??
          122. ????????? //?those?that?are?interested?in?this?event? ??
          123. ???????? for ?( int ?i?=?listeners.length- 2 ;?i>= 0 ;?i-= 2 )?{? ??
          124. ???????????? if ?(listeners[i]==MyListener. class )?{? ??
          125. ???????????????? //?Lazily?create?the?event:? ??
          126. ???????????????? if ?(myEvent?==? null )? ??
          127. ????????????????????myEvent?=? new ?MyEvent( this ?,? new ?Date());? ??
          128. ????????????????((MyListener)listeners[i+ 1 ]).doMyAction(myEvent);? ??
          129. ????????????}? ??
          130. ????????}? ??
          131. ????}? ??
          132. ??
          133. ???? /**? ?
          134. ?????*?做一些事件源應該做的有意義的事,然后通知監聽器.? ?
          135. ?????*?這里只是一個示例方法.? ?
          136. ?????*?例如:MySource如果是一個按鈕,則doSomething()就可以命名為click(),? ?
          137. ?????*?當用戶點擊按鈕時調用click()方法.? ?
          138. ?????*/ ? ??
          139. ???? public ? void ?doSomething()?{? ??
          140. ????????System.out.println( "do?something?interesting?here." );? ??
          141. ??
          142. ????????fireDoMyAction(); //通知監聽器? ??
          143. ????}? ??
          144. }???


          ?????????EventListenerList是特別需要說明的,它內部使用一個Object數組存放監聽器。但是它并不是直接存放,而是先存監聽器的class類型,然后再存監聽器本身。即存放(MyListener.class , myListener)。一個Object數組可以存放多種類型的Listener,如果還有一種監聽器AnotherListener,那么(AnotherListener.class , anotherListener)也可以存放。無論增刪都是兩個對象一同被添加或刪除。上述代碼中的listenerList.add(MyListener.class , listener)或listenerList.remove(MyListener.class , listener),以及fireDoMyAction()中的"i-=2",就是這樣操作的。

          posted on 2010-01-17 18:15 飛熊 閱讀(268) 評論(0)  編輯  收藏 所屬分類: java設計模式

          <2010年1月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 孟州市| 凭祥市| 兰州市| 枣阳市| 陆丰市| 沂南县| 屏边| 内乡县| 衡东县| 宣威市| 思茅市| 旅游| 阿巴嘎旗| 东辽县| 台北市| 高碑店市| 军事| 班戈县| 福鼎市| 分宜县| 友谊县| 八宿县| 泗洪县| 紫阳县| 扶余县| 岗巴县| 泰宁县| 景谷| 南平市| 阜平县| 三门峡市| 西乡县| 建昌县| 嘉兴市| 金乡县| 东乡| 长顺县| 金华市| 新野县| 芦溪县| 会昌县|