Java观察者模式(Java事件监听)
软件编程
373
马晶晶
: 2017-03-19 16:26:43

利用Java的观察者模式编写一个简单的Java事件监听案例

图不重要,看代码。
timg.jpg

下面直接编写代码

  1. package cn.majingjing.tzx.event;
  2. public interface EntryListener {
  3. public abstract void entryAdded(EntryEvent entryevent);
  4. public abstract void entryDeleted(EntryEvent entryevent);
  5. public abstract void entryModified(EntryEvent entryevent);
  6. }
  1. package cn.majingjing.tzx.event;
  2. public class Entry {
  3. private long id;
  4. private String name;
  5. ...省略getset方法
  6. }
  1. package cn.majingjing.tzx.event;
  2. import java.util.Collections;
  3. import java.util.Date;
  4. import java.util.Map;
  5. public class EntryEvent {
  6. public static final int ENTRY_ADDED = 100;
  7. public static final int ENTRY_DELETED = 101;
  8. public static final int ENTRY_MODIFIED = 102;
  9. private int eventType;
  10. private Entry entry;
  11. private Date date;
  12. private Map<?, ?> params;
  13. public EntryEvent(int eventType, Entry entry, Map<?, ?> params) {
  14. this.eventType = eventType;
  15. this.entry = entry;
  16. this.params = params != null ? Collections.unmodifiableMap(params) : null;
  17. date = new Date();
  18. }
  19. public int getEventType() {
  20. return eventType;
  21. }
  22. public Entry getEntry() {
  23. return entry;
  24. }
  25. public Map<?, ?> getParams() {
  26. return params;
  27. }
  28. public Date getDate() {
  29. return date;
  30. }
  31. }
  1. package cn.majingjing.tzx.event;
  2. import java.util.ArrayList;
  3. public class EntryEventDispatcher {
  4. private static EntryEventDispatcher instance = new EntryEventDispatcher();
  5. private ArrayList<EntryListener> listeners = new ArrayList<EntryListener>();
  6. public static EntryEventDispatcher getInstance() {
  7. return instance;
  8. }
  9. private EntryEventDispatcher() {
  10. String[] observers = PropertyMgr.getProperty("observers").split(",");
  11. for (String s : observers) {
  12. System.out.println(s);
  13. try {
  14. this.addListener((EntryListener) (Class.forName(s).newInstance()));
  15. } catch (InstantiationException e) {
  16. e.printStackTrace();
  17. } catch (IllegalAccessException e) {
  18. e.printStackTrace();
  19. } catch (ClassNotFoundException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }
  24. public synchronized void addListener(EntryListener listener) {
  25. System.out.println(":" + listener);
  26. if (listener == null) {
  27. System.out.println(":" + listener);
  28. throw new NullPointerException();
  29. } else {
  30. listeners.add(listener);
  31. return;
  32. }
  33. }
  34. public synchronized void removeListener(EntryListener listener) {
  35. listeners.remove(listener);
  36. }
  37. public void dispatchEvent(EntryEvent event) {
  38. // System.out.println("msgDispatchEvent");
  39. int eventType = event.getEventType();
  40. long t1 = System.currentTimeMillis();
  41. System.out.println("kaishi=" + t1);
  42. for (int i = 0; i < listeners.size(); i++) {
  43. try {
  44. EntryListener listener = (EntryListener) listeners.get(i);
  45. String name = listener.getClass().getName();
  46. name = name.substring(name.lastIndexOf('.') + 1);
  47. switch (eventType) {
  48. case EntryEvent.ENTRY_ADDED:
  49. listener.entryAdded(event);
  50. break;
  51. case EntryEvent.ENTRY_DELETED:
  52. listener.entryDeleted(event);
  53. break;
  54. case EntryEvent.ENTRY_MODIFIED:
  55. listener.entryModified(event);
  56. break;
  57. }
  58. } catch (Exception e) {
  59. // logger.error(e);
  60. }
  61. }
  62. System.out.println("cost time=" + (System.currentTimeMillis() - t1));
  63. // Profiler.end("msgDispatchEvent");
  64. }
  65. }
  1. package cn.majingjing.tzx.event.manager;
  2. import cn.majingjing.tzx.event.EntryEvent;
  3. import cn.majingjing.tzx.event.EntryListener;
  4. public class IndexManager implements EntryListener {
  5. @Override
  6. public void entryAdded(EntryEvent entryevent) {
  7. System.out.println("IndexManager add");
  8. }
  9. @Override
  10. public void entryDeleted(EntryEvent entryevent) {
  11. System.out.println("IndexManager del");
  12. }
  13. @Override
  14. public void entryModified(EntryEvent entryevent) {
  15. System.out.println("IndexManager modify");
  16. }
  17. }
  1. package cn.majingjing.tzx.event.manager;
  2. import cn.majingjing.tzx.event.EntryEvent;
  3. import cn.majingjing.tzx.event.EntryListener;
  4. public class EmailManager implements EntryListener {
  5. @Override
  6. public void entryAdded(EntryEvent entryevent) {
  7. System.out.println("EmailManager add");
  8. }
  9. @Override
  10. public void entryDeleted(EntryEvent entryevent) {
  11. System.out.println("EmailManager del");
  12. }
  13. @Override
  14. public void entryModified(EntryEvent entryevent) {
  15. System.out.println("EmailManager modify");
  16. }
  17. }
  1. package cn.majingjing.tzx.event;
  2. import java.io.IOException;
  3. import java.util.Properties;
  4. public class PropertyMgr {
  5. private static Properties props = new Properties();
  6. static {
  7. try {
  8. props.load(PropertyMgr.class.getClassLoader().getResourceAsStream("cn/majingjing/tzx/event/config.properties"));
  9. } catch (IOException e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. public static String getProperty(String key) {
  14. return props.getProperty(key);
  15. }
  16. }

配置文件 config.properties

  1. observers=cn.majingjing.tzx.event.manager.IndexManager,cn.majingjing.tzx.event.manager.ScoreManager,cn.majingjing.tzx.event.manager.EmailManager

单元测试代码

  1. package cn.majingjing.tzx;
  2. import cn.majingjing.tzx.event.Entry;
  3. import cn.majingjing.tzx.event.manager.EntryManager;
  4. public class Test {
  5. public static void main(String[] args) {
  6. EntryManager eventManager = new EntryManager();
  7. Entry entry = new Entry();
  8. entry.setId(1);
  9. entry.setName("test");
  10. eventManager.addEntry(entry);
  11. eventManager.modifyEntry(entry);
  12. eventManager.deleteEntry(entry);
  13. }
  14. }

源代码: Java观察者模式(Java事件监听)