android开发中使用java观察者模式
代码如下:
//观察者,需要用到观察者模式的类需实现此接口
public interface Observer {
void update(Object... objs);
}
//被观察者(一个抽象类,方便扩展)
public abstract class Observable {
public final ArrayList<Class<?>> obserList = new ArrayList<Class<?>>();
public <T> void registerObserver(T ob) {
if (ob == null) throw new NullPointerException();
this.registerObserver(ob.getClass());
}
public void registerObserver(Class<?> cls) {
if (cls == null) throw new NullPointerException();
synchronized(obserList) {
if (!obserList.contains(cls)) {
obserList.add(cls);
}
}
}
public <T> void unRegisterObserver(T ob) {
if (ob == null) throw new NullPointerException();
this.unRegisterObserver(ob.getClass());
}
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;
}
}
}
}
public void unRegisterAll() {
synchronized(obserList) {
obserList.clear();
}
}
public int countObservers() {
synchronized(obserList) {
return obserList.size();
}
}
public abstract void notifyObservers(Object... objs);
public abstract void notifyObserver(Class<?> cls, Object... 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等
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341