publicstaticvoidmain(String[] args){ Subject subject = new Subject(); subject.addObserver(new Watcher("001")); subject.addObserver(new Watcher("007")); subject.setChanged(); //will do nothing until setChanged() is called subject.push("My watch is ended!"); } }
@Override publicvoidupdate(Observable o, Object arg){ System.out.println("-----------------------------------------------------------"); System.out.println(id); Subject subject = (Subject) o; System.out.println("subject is : " + subject.getSubject()); System.out.println("update data is : " + (String)arg ); } }
输出示例:
1 2 3 4 5 6 7 8 9 10
My watch begins! 001 My watch begins! 007 ----------------------------------------------------------- 007 subject is : play with some fun update data is : My watch is ended! ----------------------------------------------------------- 001 subject is : play with some fun update data is : My watch is ended!
EventBus
EventBus allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another (and thus be aware of each other). It is designed exclusively to replace traditional Java in-process event distribution using explicit registration. It is not a general-purpose publish-subscribe system, nor is it intended for interprocess communication.
EventBus的优点:
无需定义接口,使用注解的形式。
可以在一个类中实现多个事件的捕获。
Due to erasure, no single class can implement a generic interface more than once with different type parameters.
@Subscribe publicvoidhierarchy(Person person){ System.out.println("-----------------------------------"); //will recieve all person and it's subtype System.out.println(person); System.out.println("\n"); }
/** * Returns all subscribers for the given listener grouped by the type of event they subscribe to. */ private Multimap<Class<?>, Subscriber> findAllSubscribers(Object listener) { Multimap<Class<?>, Subscriber> methodsInListener = HashMultimap.create(); Class<?> clazz = listener.getClass(); for (Method method : getAnnotatedMethods(clazz)) {//有缓存哦 Class<?>[] parameterTypes = method.getParameterTypes(); Class<?> eventType = parameterTypes[0]; methodsInListener.put(eventType, Subscriber.create(bus, listener, method)); } return methodsInListener; }
在subscribers的注册方法中完成了对注解@Subscribe的解析。
事件分发过程
EventBus的post方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/** * Posts an event to all registered subscribers. This method will return successfully after the * event has been posted to all subscribers, and regardless of any exceptions thrown by * subscribers. * * <p>If no subscribers have been subscribed for {@code event}'s class, and {@code event} is not * already a {@link DeadEvent}, it will be wrapped in a DeadEvent and reposted. * * @param event event to post. */ publicvoidpost(Object event){ Iterator<Subscriber> eventSubscribers = subscribers.getSubscribers(event); if (eventSubscribers.hasNext()) { dispatcher.dispatch(event, eventSubscribers); } elseif (!(event instanceof DeadEvent)) { // the event had no subscribers and was not itself a DeadEvent post(new DeadEvent(this, event)); } }