recyclerview的观察者模式分析

首先是通过adapter来帮助我们实现好的。

public abstract static class Adapter{
private final AdapterDataObservable mObservable = new AdapterDataObservable();
。。。。
}

mObservable可观察对象,发射源

static class AdapterDataObservable extends Observable {
        public boolean hasObservers() {
            return !mObservers.isEmpty();
        }

        public void notifyChanged() {
            // since onChanged() is implemented by the app, it could do anything, including
            // removing itself from {@link mObservers} - and that could cause problems if
            // an iterator is used on the ArrayList {@link mObservers}.
            // to avoid such problems, just march thru the list in the reverse order.
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onChanged();
            }
        }
。。。
}

Observable抽象泛型类,用arraylist存储观察者(接收者);只有注册和注销方法

package android.database;

import java.util.ArrayList;
public abstract class Observable {
    /**
     * The list of observers.  An observer can be in the list at most
     * once and will never be null.
     */
    protected final ArrayList mObservers = new ArrayList();

    /**
     * Adds an observer to the list. The observer cannot be null and it must not already
     * be registered.
     * @param observer the observer to register
     * @throws IllegalArgumentException the observer is null
     * @throws IllegalStateException the observer is already registered
     */
    public void registerObserver(T observer) {
        if (observer == null) {
            throw new IllegalArgumentException("The observer is null.");
        }
        synchronized(mObservers) {
            if (mObservers.contains(observer)) {
                throw new IllegalStateException("Observer " + observer + " is already registered.");
            }
            mObservers.add(observer);
        }
    }

    /**
     * Removes a previously registered observer. The observer must not be null and it
     * must already have been registered.
     * @param observer the observer to unregister
     * @throws IllegalArgumentException the observer is null
     * @throws IllegalStateException the observer is not yet registered
     */
    public void unregisterObserver(T observer) {
        if (observer == null) {
            throw new IllegalArgumentException("The observer is null.");
        }
        synchronized(mObservers) {
            int index = mObservers.indexOf(observer);
            if (index == -1) {
                throw new IllegalStateException("Observer " + observer + " was not registered.");
            }
            mObservers.remove(index);
        }
    }

    /**
     * Remove all registered observers.
     */
    public void unregisterAll() {
        synchronized(mObservers) {
            mObservers.clear();
        }
    }
}

public abstract static class AdapterDataObserver定义了观察者的发现动作的反应
AdapterDataObservable定义了如何触发事件

public void registerAdapterDataObserver(@NonNull AdapterDataObserver observer) {
            mObservable.registerObserver(observer);
        }

setAdapterInternal中,recyclerview使用它的adapter注册了观察者(此时adapter内部的mObservable代表adapter自己,作为发射源)

if (adapter != null) {
            adapter.registerAdapterDataObserver(mObserver);
            adapter.onAttachedToRecyclerView(this);
        }

观察者mObserver 是AdapterDataObserver的实现类实例,Observable模板中的T

private final RecyclerViewDataObserver mObserver = new RecyclerViewDataObserver();

private class RecyclerViewDataObserver extends AdapterDataObserver{...}

总结:adapter内置的发射源在setAdapter后,注册到了接收源,变动等事件就发射事件通知接受源。
发送源是有接收源的类型的,这样才能触发相应的事件。

image.png

你可能感兴趣的:(recyclerview的观察者模式分析)