android开发中使用java观察者模式

时间:2021-05-20

复制代码 代码如下:
//观察者,需要用到观察者模式的类需实现此接口
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 abstractvoid 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等
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章