首页 > 代码库 > 观察者模式修改
观察者模式修改
状况:遇到android程序中后入栈的一个Activity需要更新之前一个或者两个以上Activity中的数据时使用,或者需要在adapter中修改Activity或者fragment的数据和ui等
[1].[代码] [Java]代码 跳至[1]
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | //观察者,需要用到观察者模式的类需实现此接口 public interface Observer { void update(Object... objs); }
//被观察者(一个抽象类,方便扩展) public abstract class Observable { public final ArrayList<Class<?>> obserList = new ArrayList<Class<?>>(); /** Attach Observer (通过实例注册观察者) * <b>Notice:</b> ob can‘t be null ,or it will throw NullPointerException * */ public <T> void registerObserver(T ob) { if (ob == null ) throw new NullPointerException(); this .registerObserver(ob.getClass()); }
/** * Attach Observer(通过Class注册观察者) * @param cls */ public void registerObserver(Class<?> cls) { if (cls == null ) throw new NullPointerException(); synchronized (obserList) { if (!obserList.contains(cls)) { obserList.add(cls); } } } /** Unattach Observer (注销观察者) * <b>Notice:</b> * <b>It reverses with attachObserver() method</b> * */ public <T> void unRegisterObserver(T ob) { if (ob == null ) throw new NullPointerException(); this .unRegisterObserver(ob.getClass()); } /** Unattach Observer(注销观察者,有时候在未获取到实例使用) * <b>Notice:</b> * <b>It reverses with attachObserver() method</b> * */ public void unRegisterObserver(Class<?> cls) { if (cls == null ) throw new NullPointerException(); synchronized (obserList){ Iterator<Class<?>> iterator = obserList.iterator(); while (iterator.hasNext()) { if (iterator.next().getName().equals(cls.getName())){ iterator.remove(); break ; } } } } /** detach all observers */ public void unRegisterAll() { synchronized (obserList) { obserList.clear(); } } /** Ruturn the size of observers */ public int countObservers() { synchronized (obserList) { return obserList.size(); } } /** * notify all observer (通知所有观察者,在子类中实现) * @param objs */ public abstract void notifyObservers(Object... objs); /** * notify one certain observer (通知某一个确定的观察者) * @param cls * @param objs */ public abstract void notifyObserver(Class<?> cls, Object... objs); /** * notify one certain observer * @param cls * @param objs */ public abstract <T> void notifyObserver(T t, Object... objs); }
//目标被观察者 public class ConcreteObservable extends Observable {
private static ConcreteObservable instance = null ; private ConcreteObservable() {} public static synchronized ConcreteObservable getInstance() { if (instance == null ) { instance = new ConcreteObservable(); } return instance; } @Override public <T> void notifyObserver(T t, Object... objs) { // TODO Auto-generated method stub if (t == null ) throw new NullPointerException(); this .notifyObserver(t.getClass(), objs); }
@Override public void notifyObservers(Object... objs) { // TODO Auto-generated method stub for (Class<?> cls : obserList) { this .notifyObserver(cls, objs); } }
@Override //通过java反射机制实现调用 public void notifyObserver(Class<?> cls, Object... objs) { // TODO Auto-generated method stub if (cls == null ) throw new NullPointerException(); Method[] methods = cls.getDeclaredMethods(); for (Method method : methods) { if (method.getName().equals( "update" )) { try { method.invoke(cls, objs); break ; } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
//使用 (实现Observer接口) public class Text extends Activity implements Observer { public void onCreate(...) { ConcreteObservable.getInstance().registerObserver(Text. class ); .... }
//实现接口处理 public void update(Object... objs) { // 做操作,比如更新数据,更新UI等 } } |
观察者模式修改
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。