定义对象间一对多的依赖关系,每当这个对象发生改变时,其他对象都能收到通知并更新自己。
应用场景
-
关联行为场景
-
事件多级触发场景
-
跨系统的消息交换场景,例如消息队列,事件总线的处理机制。
MySubject类就是我们的主对象,Observer1和Observer2是依赖于MySubject的对象,当MySubject变化时,Observer1和Observer2必然变化。AbstractSubject类中定义着需要监控的对象列表,可以对其进行修改:增加或删除被监控对象,且当MySubject变化时,负责通知在列表内存在的对象。
public interface Observer {public void update();
}public class Observer1 implements Observer {@Overridepublic void update() {System.out.println("observer1 has received!");}
}public class Observer2 implements Observer {@Overridepublic void update() {System.out.println("observer2 has received!");}
}public interface Subject {/*增加观察者*/public void add(Observer observer);/*删除观察者*/public void del(Observer observer);/*通知所有的观察者*/public void notifyObservers();/*自身的操作*/public void operation();
}public abstract class AbstractSubject implements Subject {private Vector<Observer> vector = new Vector<Observer>();@Overridepublic void add(Observer observer) {vector.add(observer);}@Overridepublic void del(Observer observer) {vector.remove(observer);}@Overridepublic void notifyObservers() {Enumeration<Observer> enumo = vector.elements();while(enumo.hasMoreElements()){enumo.nextElement().update();}}
}public class MySubject extends AbstractSubject {@Overridepublic void operation() {System.out.println("update self!");notifyObservers();}}public class ObserverTest {public static void main(String[] args) {Subject sub = new MySubject();sub.add(new Observer1());sub.add(new Observer2());sub.operation();}}
public abstract class AbstractCursor {//定义管理器DataSetObservable mDataSetObservable = new DataSetObservable();ContentObservable mContentObservable = new ContentObservable();//注册和卸载两类观察者public void registerContentObserver(ContentObserver observer) {mContentObservable.registerObserver(observer);}public void unregisterContentObserver(ContentObserver observer) {// cursor will unregister all observers when it closeif (!mClosed) {mContentObservable.unregisterObserver(observer);}}public void registerDataSetObserver(DataSetObserver observer) {mDataSetObservable.registerObserver(observer);}public void unregisterDataSetObserver(DataSetObserver observer) {mDataSetObservable.unregisterObserver(observer);}//2类通知方法protected void onChange(boolean selfChange) {synchronized (mSelfObserverLock) {mContentObservable.dispatchChange(selfChange);if (mNotifyUri != null && selfChange) {mContentResolver.notifyChange(mNotifyUri, mSelfObserver);}}}protected void notifyDataSetChange() {mDataSetObservable.notifyChanged();}
}public abstract class Observable<T> {/*** 观察者列表*/protected final ArrayList<T> mObservers = new ArrayList<T>();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);}}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);}}public void unregisterAll() {synchronized(mObservers) {mObservers.clear();} }
}public class DataSetObservable extends Observable<DataSetObserver> {/*** 数据发生变化时,通知所有的观察者*/public void notifyChanged() {synchronized(mObservers) {for (DataSetObserver observer : mObservers) {observer.onChanged();}}}//... ... (其他方法)
}public abstract class DataSetObserver {public void onChanged() {// Do nothing}
}public class AlphabetIndexer extends DataSetObserver{/** @hide 被android系统隐藏起来了*/@Overridepublic void onChanged() {//观察到数据变化,观察者做自己该做的事情super.onChanged();mAlphaMap.clear();}
}
根据以下文章总结:
-
Java设计模式:23种设计模式全面解析(超级详细)HYPERLINK http://c.biancheng.net/design_pattern/
-
3种设计模式详解 https://www.iteye.com/blog/zz563143188-1847029
-
Android系统编程思想:设计模式https://github.com/sucese/android-open-source-project-analysis/blob/master/doc/Android%E7%B3%BB%E7%BB%9F%E8%BD%AF%E4%BB%B6%E8%AE%BE%E8%AE%A1%E7%AF%87/02Android%E7%B3%BB%E7%BB%9F%E8%BD%AF%E4%BB%B6%E8%AE%BE%E8%AE%A1%E7%AF%87%EF%BC%9A%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F.md#35-%E8%A7%82%E5%AF%9F%E8%80%85%E6%A8%A1%E5%BC%8F
-
设计模式 https://blog.csdn.net/shusheng0007/category_8638565.html
-
java设计模式 https://blog.csdn.net/qq_37909508/category_8976362.html
-
设计模式 https://www.cnblogs.com/zuoxiaolong/category/509144.html
-
设计模式 在源码中的应用 https://blog.csdn.net/qq_36970993/category_10620886.html
-
Android系统设计中存在设计模式分析 https://www.2cto.com/kf/201208/150650.html
-
Android设计模式系列 - 基于android的各种代码分析各种设计模式 https://www.cnblogs.com/qianxudetianxia/category/312863.html
?