程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java 在觀察者模式中使用泛型T的實例

java 在觀察者模式中使用泛型T的實例

編輯:關於JAVA

java 在觀察者模式中使用泛型T的實例。本站提示廣大學習愛好者:(java 在觀察者模式中使用泛型T的實例)文章只能為提供參考,不一定能成為您想要的結果。以下是java 在觀察者模式中使用泛型T的實例正文


被觀察者

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的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved