亲宝软件园·资讯

展开

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

人气:0
想了解java 在观察者模式中使用泛型T的实例的相关内容吗,在本文为您仔细讲解观察者模式 java的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:观察者模式,java,下面大家一起来学习吧。

被观察者

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的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论