The java observer pattern is used in android development

  • 2020-05-27 07:08:11
  • OfStack


// Observer, the classes that need to use the observer pattern implement this interface 
public interface Observer {
 void update(Object... objs);
}
// Observed ( 1 Abstract class, easy to extend) 
public abstract class Observable {

 public final ArrayList<Class<?>> obserList = new ArrayList<Class<?>>();

 /** Attach Observer  (register an observer by instance) 
  * <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 (by Class Registered observer) 
  * @param cls
  */
 public void registerObserver(Class<?> cls) {
  if (cls == null) throw new NullPointerException();
  synchronized(obserList) {
   if (!obserList.contains(cls)) {
    obserList.add(cls);
   }
  }
 }

 /** Unattach Observer  (unregister 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 (unregister the observer, sometimes used without getting an instance) 
  * <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  (notifies all observers, implemented in subclasses) 
  * @param objs
  */
 public abstract void notifyObservers(Object... objs);

 /**
  * notify one certain observer  Notice something, 1 A 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);
}
// Target observed 
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  // through java The reflection mechanism implements the call 
 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();
    }
   }
  }
 }

}
// use  ( implementation Observer Interface) 
public class Text extends Activity implements Observer {
   public void onCreate(...) {
       ConcreteObservable.getInstance().registerObserver(Text.class);
       ....
   }
   // Implement interface handling 
   public void update(Object... objs) {
       //  Do operations, like update data, update UI Etc. 
   }
}


Related articles: