欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-06-29 09:19:57
复制代码 代码如下://观察者,需要用到观察者模式的类需实现此接口public interface observer { void update(object.....

复制代码 代码如下:

//观察者,需要用到观察者模式的类需实现此接口
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等
   }
}