一、观察者模式
1.定义
观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己。
(1)抽象主题(Subject)角色:
抽象主题角色把所有对观察者对象的引用保存在一个聚集(比如ArrayList对象)里,每个主题都可以有任何数量的观察者。
抽象主题提供一个接口,可以增加和删除观察者对象,抽象主题角色又叫做抽象被观察者(Observable)角色。可以定义为接口或者抽象类,一般定义为抽象类。
(2)具体主题(ConcreteSubject)角色:
将有关联状态存入具体观察者对象,在具体主题的内部状态改变时,给所有登记过的观察者发出通知(调用抽象主题的notifyObserver)。具体主题角色又叫做具体观察者(Concrete Observable)角色。
(3)抽象观察者(Observer)角色
一个接口。为所有的具体观察者定义一个接口,在得到主题的通知时更新自己,这个接口叫做更新接口。
(4)具体观察者(ConcreteObserver)角色
是抽象观察者的实现类,实现父类的方法更新自己。存储与主题的状态自恰的状态。具体观察者角色实现抽象观察者角色所要求的更新接口,以便使本身的状态相协调。如果需要,具体观察者角色可以保持一个指向具体主题对象的引用。
2.简单示例
抽象主题
public abstract class Observable {
private static final String TAG = Observable.class.getSimpleName();
// 观察者集合
private List<Observer> observers = new ArrayList<>();
// 注册观察者对象
public void add(Observer observer) {
observers.add(observer);
Log.i(TAG, "Add an observer");
}
// 删除观察者对象
public void remove(Observer observer) {
observers.remove(observer);
Log.i(TAG, "Remove an observer");
}
// 删除所有观察者对象
public void removeAll(){
observers.clear();
}
// 通知所有观察者更新状态
public void nodifyObservers(String strUpdate) {
// 遍历通知
for (Observer observer : observers) {
observer.update(strUpdate);
}
}
}
具体主题
public class ConcreteObservable extends Observable {
// 调用即可通知所有的观察者
public void change(String strChange){
nodifyObservers(strChange);
}
}
抽象观察者
public interface Observer {
/**
* 更新方法
* @param str
*/
public abstract void update(String str);
}
具体观察者
public class ConcreteObserver implements Observer {
private static final String TAG = ConcreteObserver.class.getSimpleName();
@Override
public void update(String str) {
Log.i(TAG, "Receive new Message: " + str);
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
ConcreteObserver observer = new ConcreteObserver();
ConcreteObservable observable = new ConcreteObservable();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addObserver = (Button) findViewById(R.id.add_observer);
Button update = (Button) findViewById(R.id.update);
addObserver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
observable.add(observer);
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
observable.change("Hello World!");
}
});
}
}
运行结果:
I/Observable: Add an observer
I/ConcreteObserver: Receive new Message: Hello World!
二、RecyclerView源码中的观察者模式
我们使用RecyclerView的时候,在每次更新了RecyclerView的数据后通常调用notifyDataSetChanged(),notifyItemChanged(),notifyItemRangeChanged(),notifyItemInserted(),notifyItemRemoved(),notifyItemMoved()等方法来更新我们的视图。而这些更新方法就是用到了我们所说的观察者模式,我们就看一下RecyclerView源码中的观察者模式是怎么实现的。
public static abstract class Adapter<VH extends ViewHolder> {
private final AdapterDataObservable mObservable = new AdapterDataObservable();
private boolean mHasStableIds = false;
public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);
public abstract void onBindViewHolder(VH holder, int position);
public void onBindViewHolder(VH holder, int position, List<Object> payloads) {
onBindViewHolder(holder, position);
}
public final void bindViewHolder(VH holder, int position) {
holder.mPosition = position;
if (hasStableIds()) {
holder.mItemId = getItemId(position);
}
holder.setFlags(ViewHolder.FLAG_BOUND,
ViewHolder.FLAG_BOUND | ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID
| ViewHolder.FLAG_ADAPTER_POSITION_UNKNOWN);
TraceCompat.beginSection(TRACE_BIND_VIEW_TAG);
onBindViewHolder(holder, position, holder.getUnmodifiedPayloads());
holder.clearPayload();
final ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
if (layoutParams instanceof RecyclerView.LayoutParams) {
((LayoutParams) layoutParams).mInsetsDirty = true;
}
TraceCompat.endSection();
}
public int getItemViewType(int position) {
return 0;
}
public void setHasStableIds(boolean hasStableIds) {
if (hasObservers()) {
throw new IllegalStateException("Cannot change whether this adapter has " +
"stable IDs while the adapter has registered observers.");
}
mHasStableIds = hasStableIds;
}
public long getItemId(int position) {
return NO_ID;
}
public final boolean hasStableIds() {
return mHasStableIds;
}
public void onViewRecycled(VH holder) {
}
public boolean onFailedToRecycleView(VH holder) {
return false;
}
public void onViewAttachedToWindow(VH holder) {
}
public final boolean hasObservers() {
return mObservable.hasObservers();
}
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
}
public final void notifyDataSetChanged() {
mObservable.notifyChanged();
}
public final void notifyItemChanged(int position) {
mObservable.notifyItemRangeChanged(position, 1);
}
public final void notifyItemChanged(int position, Object payload) {
mObservable.notifyItemRangeChanged(position, 1, payload);
}
public final void notifyItemRangeChanged(int positionStart, int itemCount) {
mObservable.notifyItemRangeChanged(positionStart, itemCount);
}
public final void notifyItemRangeChanged(int positionStart, int itemCount, Object payload) {
mObservable.notifyItemRangeChanged(positionStart, itemCount, payload);
}
public final void notifyItemInserted(int position) {
mObservable.notifyItemRangeInserted(position, 1);
}
public final void notifyItemRangeInserted(int positionStart, int itemCount) {
mObservable.notifyItemRangeInserted(positionStart, itemCount);
}
public final void notifyItemRemoved(int position) {
mObservable.notifyItemRangeRemoved(position, 1);
}
public final void notifyItemRangeRemoved(int positionStart, int itemCount) {
mObservable.notifyItemRangeRemoved(positionStart, itemCount);
}
}
接下来,我们跟进到notifyDataSetChanged()方法看看,mObservable.notifyChanged()进到AdapterDataObservable中。
static class AdapterDataObservable extends Observable<AdapterDataObserver> {
public boolean hasObservers() {
return !mObservers.isEmpty();
}
public void notifyChanged() {
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onChanged();
}
}
public void notifyItemRangeChanged(int positionStart, int itemCount) {
notifyItemRangeChanged(positionStart, itemCount, null);
}
public void notifyItemRangeChanged(int positionStart, int itemCount, Object payload) {
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onItemRangeChanged(positionStart, itemCount, payload);
}
}
public void notifyItemRangeInserted(int positionStart, int itemCount) {
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onItemRangeInserted(positionStart, itemCount);
}
}
public void notifyItemRangeRemoved(int positionStart, int itemCount) {
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onItemRangeRemoved(positionStart, itemCount);
}
}
public void notifyItemMoved(int fromPosition, int toPosition) {
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onItemRangeMoved(fromPosition, toPosition, 1);
}
}
}
从AdapterDataObservable的notifyChanged()方法可知,就是遍历所有的观察者,调用它们的onChange()方法来进行通知。而且我们可以发现,其实不光是notifyDataSetChanged()方法,其它刷新方法都是通过一样的模式去通知观察者的。
如此,我们知道了被观察者(Observable)是怎么实现的,再来看下观察者(Observer)的实现。RecyclerView关联数据的时候都是通过调用setAdapter()方法实现的,我们看下setAdapter()方法。
public void setAdapter(Adapter adapter) {
setLayoutFrozen(false);
setAdapterInternal(adapter, false, true);
requestLayout();
}
该方法把adapter传进了setAdapterInternal()方法,我们再看下这个方法。
private void setAdapterInternal(Adapter adapter, boolean compatibleWithPrevious,
boolean removeAndRecycleViews) {
if (mAdapter != null) {
mAdapter.unregisterAdapterDataObserver(mObserver);
mAdapter.onDetachedFromRecyclerView(this);
}
if (!compatibleWithPrevious || removeAndRecycleViews) {
removeAndRecycleViews();
}
mAdapterHelper.reset();
final Adapter oldAdapter = mAdapter;
mAdapter = adapter;
if (adapter != null) {
adapter.registerAdapterDataObserver(mObserver);
adapter.onAttachedToRecyclerView(this);
}
if (mLayout != null) {
mLayout.onAdapterChanged(oldAdapter, mAdapter);
}
mRecycler.onAdapterChanged(oldAdapter, mAdapter, compatibleWithPrevious);
mState.mStructureChanged = true;
markKnownViewsInvalid();
}
在setAdapter的时候会构建一个RecyclerViewDataObserver,也就是我们的观察者(RecyclerViewDataObserver),然后将它注册到被观察者中,我们找到了被观察者和观察者之间的关联。RecyclerViewDataObserver继承自AdapterDataObserver,那我们看下AdapterDataObserver。
public static abstract class AdapterDataObserver {
public void onChanged() {
// Do nothing
}
public void onItemRangeChanged(int positionStart, int itemCount) {
// do nothing
}
public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
// fallback to onItemRangeChanged(positionStart, itemCount) if app
// does not override this method.
onItemRangeChanged(positionStart, itemCount);
}
public void onItemRangeInserted(int positionStart, int itemCount) {
// do nothing
}
public void onItemRangeRemoved(int positionStart, int itemCount) {
// do nothing
}
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
// do nothing
}
}
父类AdapterDataObserver都是空方法,没有具体的实现,是抽象观察者角色。我们再来看下RecyclerViewDataObserver的具体实现。
private class RecyclerViewDataObserver extends AdapterDataObserver {
RecyclerViewDataObserver() {
}
@Override
public void onChanged() {
assertNotInLayoutOrScroll(null);
mState.mStructureChanged = true;
setDataSetChangedAfterLayout();
if (!mAdapterHelper.hasPendingUpdates()) {
requestLayout();
}
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
assertNotInLayoutOrScroll(null);
if (mAdapterHelper.onItemRangeChanged(positionStart, itemCount, payload)) {
triggerUpdateProcessor();
}
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
assertNotInLayoutOrScroll(null);
if (mAdapterHelper.onItemRangeInserted(positionStart, itemCount)) {
triggerUpdateProcessor();
}
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
assertNotInLayoutOrScroll(null);
if (mAdapterHelper.onItemRangeRemoved(positionStart, itemCount)) {
triggerUpdateProcessor();
}
}
@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
assertNotInLayoutOrScroll(null);
if (mAdapterHelper.onItemRangeMoved(fromPosition, toPosition, itemCount)) {
triggerUpdateProcessor();
}
}
void triggerUpdateProcessor() {
if (POST_UPDATES_ON_ANIMATION && mHasFixedSize && mIsAttached) {
ViewCompat.postOnAnimation(RecyclerView.this, mUpdateChildViewsRunnable);
} else {
mAdapterUpdateDuringMeasure = true;
requestLayout();
}
}
}
当RecyclerView的数据发生变化时,调用Adapter的notifyDataSetChanged()方法,这个方法又会调用AdapterDataObservable的notifyChanged()方法。这个方法又会调用所有观察者的onChanged()方法,在onChanged方法里对RecyclerView进行重新布局实现刷新界面。
瞎总结
我们在为RecyclerView创建Adapter的时候,构建了一个具体观察者RecyclerViewDataObserver(继承自抽象观察者AdapterDataObserver),并注册到AdapterDataObservable。当我们调用notifyDataSetChanged()及其它刷新方法的时候,其实就是调用了被观察者AdapterDataObservable的notifyChanged()方法,而该方法会遍历所有观察者的onChanged()方法,最终调用RecyclerView重新布局从而实现界面刷新。