/** 抽象的主题*/
public abstract class Subject { ArrayList<Observer> observers=new ArrayList<Observer>(); public abstract void Notify(); public abstract void addObserver(Observer ob); public abstract void removeObserver(Observer ob); }
/** * 观察者抽象接口 * @author pdan * @da2016年5月11日 */ public abstract class Observer { public abstract void Update(); }
/** * 具体主题; * @author pdan * @da2016年5月11日 */ public class ConcreateSubject extends Subject{ public String status="aaa"; @Override public void Notify() { for(Observer ob : observers){ ob.Update(); } } @Override public void addObserver(Observer ob) { observers.add(ob); } @Override public void removeObserver(Observer ob) { observers.remove(ob); } }
/** * 具体的观察者 * @author pdan * @da2016年5月12日 */ public class ConcreateObserver extends Observer{ ConcreateSubject subject; public ConcreateObserver(ConcreateSubject subject) { this.subject=subject; } @Override public void Update() { System.out.println(subject.status); } }
Android中观察这模式的存在: 暂时还没理解...
package AndroidObserver; /*** * 抽象的观察者。 * @author pdan * @da2016年5月12日 */ public abstract class BaseAdapter { private final DataSetObservable mDataSetObservable = new DataSetObservable(); public void registerDataSetObserver(DataSetObserver observer) { mDataSetObservable.registerObserver(observer); } public void unregisterDataSetObserver(DataSetObserver observer) { mDataSetObservable.unregisterObserver(observer); } /** * Notifies the attached observers that the underlying data has been changed * and any View reflecting the data set should refresh itself. */ public void notifyDataSetChanged() { mDataSetObservable.notifyChanged(); } /** * Notifies the attached observers that the underlying data is no longer valid * or available. Once invoked this adapter is no longer valid and should * not report further data set changes. */ public void notifyDataSetInvalidated() { mDataSetObservable.notifyInvalidated(); } public boolean areAllItemsEnabled() { return true; } public boolean isEnabled(int position) { return true; } public int getItemViewType(int position) { return 0; } public int getViewTypeCount() { return 1; } /** * * @return Count of items. */ abstract int getCount(); abstract Object getItem(int position); abstract ListView getView(int position, ListView convertView); }
package AndroidObserver; /**抽象的观察者。*/ public abstract class DataSetObserver { /** * This method is called when the entire data set has changed, * most likely through a call to {@link Cursor#requery()} on a {@link Cursor}. * 当主题发生改变的时候调用此方法 */ public void onChanged() { // Do nothing } /** * This method is called when the entire data becomes invalid, * most likely through a call to {@link Cursor#deactivate()} or {@link Cursor#close()} on a * {@link Cursor}. */ public void onInvalidated() { // Do nothing } }
package AndroidObserver; import java.util.ArrayList; /**抽象的观察者*/ public abstract class Observable<T> { /** * The list of observers. An observer can be in the list at most * once and will never be null. */ protected final ArrayList<T> mObservers = new ArrayList<T>(); /** * 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 observer */ public void unregisterAll() { synchronized(mObservers) { mObservers.clear(); } } }
package AndroidObserver; /**具体的观察者。*/ public class DataSetObservable extends Observable<DataSetObserver> { /** * Invokes onChanged on each observer. Called when the data set being observed has * changed, and which when read contains the new state of the data. */ public void notifyChanged() { synchronized(mObservers) { // 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(); } } } /** * Invokes onInvalidated on each observer. Called when the data set being monitored * has changed such that it is no longer valid. */ public void notifyInvalidated() { synchronized (mObservers) { for (int i = mObservers.size() - 1; i >= 0; i--) { mObservers.get(i).onInvalidated(); } } } }
package AndroidObserver; import java.util.ArrayList; public class ListAdapter extends BaseAdapter{ private ArrayList<String> strs; public ListAdapter(ArrayList<String> strs) { this.strs=strs; } @Override int getCount() { // TODO Auto-generated method stub return 0; } @Override Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override ListView getView(int position, ListView convertView) { // TODO Auto-generated method stub return null; } }
package AndroidObserver; public class ListView { private ListAdapter mAdapter; private DataSetObserver mDataSetObserver; public void setAdapter(ListAdapter adapter) { if (mAdapter != null && mDataSetObserver != null) { mAdapter.unregisterDataSetObserver(mDataSetObserver); } mAdapter = adapter; } }
package AndroidObserver; import java.util.ArrayList; public class Test { public static void main(String[] args) { ArrayList<String> ars = new ArrayList<String>(); ars.add("1"); ars.add("2"); ars.add("3"); ListView listViwe = new ListView(); ListAdapter adapter = new ListAdapter(ars); listViwe.setAdapter(adapter); ars.remove(1); adapter.notifyDataSetChanged(); } }