我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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

被观察者

创新互联公司专注于企业全网营销推广、网站重做改版、婺城网站定制设计、自适应品牌网站建设、H5页面制作商城网站建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为婺城等各大城市提供网站开发制作服务。

public class Observable {

  List observers = new ArrayList();

  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()}.
   * 

* 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 {
  public void update(Observable observable, T data);
}

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


网站标题:java在观察者模式中使用泛型T的实例
URL分享:http://shouzuofang.com/article/giecdp.html

其他资讯