最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年02月21日 11:05:05   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇java 在觀察者模式中使用泛型T的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

被觀察者

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的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • ConditionalOnProperty配置swagger不生效問(wèn)題及解決

    ConditionalOnProperty配置swagger不生效問(wèn)題及解決

    這篇文章主要介紹了ConditionalOnProperty配置swagger不生效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 詳解三種java實(shí)現(xiàn)多線程的方式

    詳解三種java實(shí)現(xiàn)多線程的方式

    數(shù)據(jù)時(shí)代的到來(lái),多線程一直都是比較關(guān)心的問(wèn)題之一,這篇文章介紹了JAVA實(shí)現(xiàn)多線程的三種方法,有需要的朋友可以參考一下
    2015-08-08
  • Java 最優(yōu)二叉樹(shù)的哈夫曼算法的簡(jiǎn)單實(shí)現(xiàn)

    Java 最優(yōu)二叉樹(shù)的哈夫曼算法的簡(jiǎn)單實(shí)現(xiàn)

    這篇文章主要介紹了Java 最優(yōu)二叉樹(shù)的哈夫曼算法的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 關(guān)于Assert.assertEquals報(bào)錯(cuò)的問(wèn)題及解決

    關(guān)于Assert.assertEquals報(bào)錯(cuò)的問(wèn)題及解決

    這篇文章主要介紹了關(guān)于Assert.assertEquals報(bào)錯(cuò)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 詳解Spring中的@PropertySource注解使用

    詳解Spring中的@PropertySource注解使用

    這篇文章主要介紹了Spring的@PropertySource注解使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • 在Java和PostgreSQL枚舉之間轉(zhuǎn)換的通用方法

    在Java和PostgreSQL枚舉之間轉(zhuǎn)換的通用方法

    枚舉類型(enum)是一種方便的數(shù)據(jù)類型,允許我們指定一個(gè)常量列表,對(duì)象字段或數(shù)據(jù)庫(kù)列可以設(shè)置為該列表中的值,在本文中,我將回顧處理Java和PostgreSQL枚舉轉(zhuǎn)換的通用方法,需要的朋友可以參考下
    2023-10-10
  • SpringMvc+Mybatis+Pagehelper分頁(yè)詳解

    SpringMvc+Mybatis+Pagehelper分頁(yè)詳解

    這篇文章主要介紹了SpringMvc+Mybatis+Pagehelper分頁(yè)詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下的相關(guān)資料
    2017-01-01
  • Java程序進(jìn)程起來(lái)了但是不打印日志的原因分析

    Java程序進(jìn)程起來(lái)了但是不打印日志的原因分析

    這篇文章主要介紹了Java程序進(jìn)程起來(lái)了但是不打印日志的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Java簡(jiǎn)單實(shí)現(xiàn)UDP和TCP的示例

    Java簡(jiǎn)單實(shí)現(xiàn)UDP和TCP的示例

    下面小編就為大家?guī)?lái)一篇Java簡(jiǎn)單實(shí)現(xiàn)UDP和TCP的示例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • 一文搞懂Java SPI機(jī)制的原理與使用

    一文搞懂Java SPI機(jī)制的原理與使用

    Java 程序員在日常工作中經(jīng)常會(huì)聽(tīng)到 SPI,而且很多框架都使用了 SPI 的技術(shù),那么問(wèn)題來(lái)了,到底什么是 SPI 呢?今天小編就帶大家好好了解一下 SPI
    2022-10-10

最新評(píng)論

蓬安县| 沙坪坝区| 于都县| 筠连县| 花莲市| 河源市| 宿州市| 耒阳市| 盐边县| 禹城市| 湘潭市| 翁牛特旗| 黄梅县| 汕头市| 东乡族自治县| 张掖市| 吉木萨尔县| 聂荣县| 金溪县| 台北市| 共和县| 云霄县| 望奎县| 合阳县| 夹江县| 辛集市| 丰城市| 游戏| 麟游县| 蒙山县| 枝江市| 晋江市| 通江县| 寻甸| 砀山县| 宝兴县| 威宁| 新巴尔虎左旗| 界首市| 图木舒克市| 佳木斯市|