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

java 在观察者模式中使用泛型T的实例

程序员文章站 2024-03-05 23:23:07
被观察者 public class observable { list observers = n...

被观察者

public class observable<t> {

  list<observer> observers = new arraylist<observer>();

  boolean changed = false;


  /**
   * adds the specified observer to the list of observers. if it is already
   * registered, it is not added a second time.
   *
   * @param observer
   *      the observer to add.
   */
  public void addobserver(observer observer) {
    if (observer == null) {
      throw new nullpointerexception("observer == null");
    }
    synchronized (this) {
      if (!observers.contains(observer))
        observers.add(observer);
    }
  }

  /**
   * clears the changed flag for this {@code observable}. after calling
   * {@code clearchanged()}, {@code haschanged()} will return {@code false}.
   */
  protected void clearchanged() {
    changed = false;
  }

  /**
   * returns the number of observers registered to this {@code observable}.
   *
   * @return the number of observers.
   */
  public int countobservers() {
    return observers.size();
  }

  /**
   * removes the specified observer from the list of observers. passing null
   * won't do anything.
   *
   * @param observer
   *      the observer to remove.
   */
  public synchronized void deleteobserver(java.util.observer observer) {
    observers.remove(observer);
  }

  /**
   * removes all observers from the list of observers.
   */
  public synchronized void deleteobservers() {
    observers.clear();
  }

  /**
   * returns the changed flag for this {@code observable}.
   *
   * @return {@code true} when the changed flag for this {@code observable} is
   *     set, {@code false} otherwise.
   */
  public boolean haschanged() {
    return changed;
  }

  /**
   * if {@code haschanged()} returns {@code true}, calls the {@code update()}
   * method for every observer in the list of observers using null as the
   * argument. afterwards, calls {@code clearchanged()}.
   * <p>
   * equivalent to calling {@code notifyobservers(null)}.
   */
  public void notifyobservers() {
    notifyobservers(null);
  }

  /**
   * if {@code haschanged()} returns {@code true}, calls the {@code update()}
   * method for every observer in the list of observers using the specified
   * argument. afterwards calls {@code clearchanged()}.
   *
   * @param data
   *      the argument passed to {@code update()}.
   */
  public void notifyobservers(t data) {
    int size = 0;
    observer[] arrays = null;
    synchronized (this) {
      if (haschanged()) {
        clearchanged();
        size = observers.size();
        arrays = new observer[size];
        observers.toarray(arrays);
      }
    }
    if (arrays != null) {
      for (observer observer : arrays) {
        observer.update(this, data);
      }
    }
  }

  /**
   * sets the changed flag for this {@code observable}. after calling
   * {@code setchanged()}, {@code haschanged()} will return {@code true}.
   */
  protected void setchanged() {
    changed = true;
  }
}

观察者

public interface observer<t> {
  public void update(observable<t> observable, t data);
}

以上这篇java 在观察者模式中使用泛型t的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。