标签:android style blog http io color ar os 使用
public abstract class AbsHTTPRequest { private final WeakHashMap<OnChangedListener, Boolean> mListeners = new WeakHashMap<OnChangedListener, Boolean>(); public interface OnChangedListener { void onDataChanged(); } /*HTTP's response*/ public abstract void onResponse(); public final void addListener(OnChangedListener listener) { mListeners.put(listener, true); } public final void removeListener(OnChangedListener listener) { mListeners.remove(listener); } protected final void notifyDataChanged() { Set<OnChangedListener> keys = mListeners.keySet(); if(keys != null) { Iterator<OnChangedListener> iterator = keys.iterator(); while(iterator.hasNext()) { iterator.next().onDataChanged(); } } } }具体的主题角色( 被观察者),实现方式如下:
public class LoginRequest extends AbsHTTPRequest implements OnChangedListener{ public void onResponse(){ addListener(this); notifyDataChanged(); } @Override public void onDataChanged() { // TODO Auto-generated method stub System.out.println("LoginRequest"); } }使用观察者模式有一个弊病就是部件之间的耦合度太高,所有的主题角色都需要实现同一个interface。观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。如果主题角色被注册的observer越多,那么需要实现的interface也就越多,接口方法数量也就越多。
SubscribMethod.java final class SubscriberMethod { final Method method; /*Method类型的method成员表示这个onEvent,即事件处理函数。同时也包含订阅源*/ final ThreadMode threadMode; final Class<?> eventType; /*事件的对象,用户自定义Object*/ ... ... ... ... ... ... ... ... ... ... ... ... }Subscription.java
final class Subscription { final Object subscriber; /*订阅源Subscriber,即调用register注册的对象*/ final SubscriberMethod subscriberMethod; /**/ final int priority; ... ... ... ... ... ... ... ... ... ... ... ... }
/*EventType -> List<Subscription>,事件到订阅对象之间的映射*/ private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType; /* Subscriber -> List<EventType>,订阅对象到它订阅的的所有事件的映射关系*/ private final Map<Object, List<Class<?>>> typesBySubscriber;注册流程:在调用register函数时,EventBus类有多个重载的register函数,但是作者更倾向于使用register(this);含有 多个参数的register函数中,明确标注了@deprecated,原创作者不建议使用。从代码:
public void register(Object subscriber) { register(subscriber, DEFAULT_METHOD_NAME, false, 0); }可以观察到,所有重载的register函数,都调用到了
private synchronized void register(Object subscriber, String methodName, boolean sticky, int priority) { List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriber.getClass(),methodName); for (SubscriberMethod subscriberMethod : subscriberMethods) { subscribe(subscriber, subscriberMethod, sticky, priority); } }其中注册函数register,默认参数DEFAULT_METHOD_NAME为函数名称"onEvent",在java放射机制中,所有的事件处理函数名称 统一为“onEvent”,仅仅参数不一致。onEvent的参数为用户自定义的对象。
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod, boolean sticky, int priority)进行处理。
/** Finds all Class objects including super classes and interfaces. */ private List<Class<?>> findEventTypes(Class<?> eventClass) { synchronized (eventTypesCache) { List<Class<?>> eventTypes = eventTypesCache.get(eventClass); if (eventTypes == null) { eventTypes = new ArrayList<Class<?>>(); Class<?> clazz = eventClass; while (clazz != null) { eventTypes.add(clazz); addInterfaces(eventTypes, clazz.getInterfaces()); clazz = clazz.getSuperclass(); } eventTypesCache.put(eventClass, eventTypes); } return eventTypes; } }其作用,就是把这个事件类的对象、实现的接口及父类的类对象存到一个List中返回,根据list中的eventTypes,遍历subscriptionsByEventType,获取订阅源对象,进行逐一的调用事件函数。
标签:android style blog http io color ar os 使用
原文地址:http://blog.csdn.net/coder80/article/details/40826635