javaSE觀察者模式Observer和Observable--相關api的翻譯

相關api的翻譯,翻譯可能存在誤差,有問題謝謝留言。譯文都是在原文下方可自行參考。

Java™ Platform, Standard Edition 8
API Specification

 

java.util Interface Observer

  • public interface Observer
    A class can implement the Observer interface when it wants to be informed of changes in observable objects.

    任何類都可以實現Observer接口,實現該接口的類,可以獲取相關被觀察者(observable)對象的更改情況。

    Since:

    JDK1.0

    See Also:

    Observable

    • Method Summary

      All MethodsInstance MethodsAbstract Methods
      Modifier and Type Method and Description
      void update(Observable o, Object arg)
      This method is called whenever the observed object is changed.
      當被觀察者(observed)對象發生變動時,該方法將被調用。
    • Method Detail

      • update

        void update(Observable o,
                    Object arg)
        This method is called whenever the observed object is changed. An application calls an Observable object's notifyObservers method to have all the object's observers notified of the change.

        當被觀察者(observed)對象發生變動時,該方法將被調用。當程序調用被觀察者(Observale)對象的notifyObservers方法時,這個被觀察者的所有觀察者都會收到對象改變的通知。

        Parameters:

        o - the observable object. 接受被觀察的對象

        arg - an argument passed to the notifyObservers method. 被觀察者notifyObservers方法傳遞過來的一個Object參數。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

java.util   Class Observable


  • public class Observable
    extends Object
    This class represents an observable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have observed.

    Observalbe代表一個被觀察的對象,或者叫模型視圖樣式中的“數據”。它能被子類化,其子類代表程序想要觀察的對象。

    Since:

    JDK1.0

    See Also:

    notifyObservers()notifyObservers(java.lang.Object)Observer

    Observer.update(java.util.Observable, java.lang.Object)

  • An observable object can have one or more observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the Observable's notifyObservers method causes all of its observers to be notified of the change by a call to their update method.
    一個被觀察者對象可以擁有一個或多個觀察者。觀察者可以是任意實現了Observer接口的對象。當一個被觀察實例發生改變時,程序會調用Observalbe(被觀察對象)的notifyObservers方法,使觀察者收到被觀察者改變的通知讓觀察者調用update方法。

    The order in which notifications will be delivered is unspecified. The default implementation provided in the Observable class will notify Observers in the order in which they registered interest, but subclasses may change this order, use no guaranteed order, deliver notifications on separate threads, or may guarantee that their subclass follows this order, as they choose.
    消息發送的次序是未被指明的。默認情況下Observable的子類將使用與觀察者註冊的相關的順序。但是子類有可能改變這一順序,如使用沒有保證的順序,在分離的線程中發送通知;或者保證子類自己允許的順序。

    Note that this notification mechanism has nothing to do with threads and is completely separate from the wait and notify mechanism of class Object.
    通知的機制與線程是沒有關係的,而且與Object的wait和notify機制也是分離的。

    When an observable object is newly created, its set of observers is empty. Two observers are considered the same if and only if the equalsmethod returns true for them.
    當創建一個新的觀察者對象時,它的觀察者集是爲空的。只有在equals方法返回爲true時才認爲兩個觀察者是相同的(指的是被觀察者在添加觀察者時對觀察者的判定)。

    • Constructor Summary

      Constructors
      Constructor and Description
      Observable()
      Construct an Observable with zero Observers.構建有零個觀察者的被觀察者對象。
    • Method Summary

      All MethodsInstance MethodsConcrete Methods
      Modifier and Type Method and Description
      void addObserver(Observer o)
      Adds an observer to the set of observers for this object,
       provided that it is not the same as some observer already in the set.
      增加一個觀察者到被觀察者的觀察者集中,添加的觀察者必須是觀察
      者集中不存在的觀察者。
      protected void clearChanged()
      Indicates that this object has no longer changed, 
      or that it has already notified all of its observers 
      of its most recent change,
       so that the hasChanged method will now return false.
      標識被觀察者對象不再改變,
      或者已近通知了所有觀察者最近的改變,
      那麼將導致hasChanged方法返回false。
      int countObservers()
      Returns the number of observers of this Observable object.
      返回觀察者集中觀察者的總個數。
      void deleteObserver(Observer o)
      Deletes an observer from the set of observers of this object.
      從觀察者集中刪除一個指定的觀察者。
      void deleteObservers()
      Clears the observer list 
      so that this object no longer has any observers.
      清空觀察者集。
      boolean hasChanged()
      Tests if this object has changed.
      測試被觀察者對象是否有改變
      (相當於獲取當前被觀察者是否發生改變的狀態)。
      void notifyObservers()
      If this object has changed, as indicated by the hasChanged method, 
      then notify all of its observers 
      and then call the clearChanged method to indicate 
      that this object has no longer changed.
      如果被觀察者發生改變,就如通過調用hasChanged方法一樣
      ,然後通知所有的觀察者
      ,然後調用clearChanged方法讓該對象不再改變。
      void notifyObservers(Object arg)
      If this object has changed, as indicated by the hasChanged method, 
      then notify all of its observers and then call the clearChanged method 
      to indicate that this object has no longer changed.
      如果被觀察者發生改變,就如通過調用hasChanged方法一樣
      ,然後通知所有的觀察者
      ,然後調用clearChanged方法讓該對象不再改變。
      protected void setChanged()
      Marks this Observable object as having been changed;
       the hasChanged method will now returntrue.
      標記被觀察者對象已經發生改變,hasChanged方法將返回true。
    • Constructor Detail

      • Observable

        public Observable()
        Construct an Observable with zero Observers.構建有零個觀察者的被觀察者對象。
    • Method Detail

      • addObserver

        public void addObserver(Observer o)
        Adds an observer to the set of observers for this object, provided that it is not the same as some observer already in the set. The order in which notifications will be delivered to multiple observers is not specified. See the class comment.
        增加一個觀察者到被觀察者的觀察者集中,添加的觀察者必須是觀察者集中不存在的觀察者。這個通知多個觀察者的順序是沒有被指定的。參見之前的類註釋。

        Parameters:

        o - an observer to be added. 一個將被添加觀察者對象。

        Throws:

        NullPointerException - if the parameter o is null.如果傳遞過來的觀察者對象爲空,則會產生該異常。

      • deleteObserver

        public void deleteObserver(Observer o)
        Deletes an observer from the set of observers of this object. Passing null to this method will have no effect.                                        從被觀察者的觀察者集中刪除o對象。如果o對象爲null那麼該方法將不會產生任何效果。

        Parameters:

        o - the observer to be deleted.需要刪除的Observer對象。

      • notifyObservers

        public void notifyObservers()
        If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChangedmethod to indicate that this object has no longer changed.
        如果被觀察者發生改變,就如通過調用hasChanged方法一樣
        ,然後通知所有的觀察者
        ,然後調用clearChanged方法讓該對象不再改變。

        Each observer has its update method called with two arguments: this observable object and null. In other words, this method is equivalent to:

        notifyObservers(null)
        每個觀察這都將調用有兩個參數的update方法,其中兩個參數一個是Observable對象一個爲null。換句話說該方法等同於notifyObservers(null).

        See Also:

        clearChanged()hasChanged()Observer.update(java.util.Observable, java.lang.Object)

      • notifyObservers

        public void notifyObservers(Object arg)
        If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChangedmethod to indicate that this object has no longer changed.
        如果被觀察者發生改變,就如通過調用hasChanged方法一樣
        ,然後通知所有的觀察者
        ,然後調用clearChanged方法讓該對象不再改變。

        Each observer has its update method called with two arguments: this observable object and the arg argument.
        每個觀察這都將調用有兩個參數的update方法,其中兩個參數一個是Observable對象一個爲arg。

        Parameters:

        arg - any object.一個Object對象

        See Also:

        clearChanged()hasChanged()Observer.update(java.util.Observable, java.lang.Object)

      • deleteObservers

        public void deleteObservers()
        Clears the observer list so that this object no longer has any observers.
        清空觀察者集,不再擁有任何觀察者。
      • setChanged

        protected void setChanged()
        Marks this Observable object as having been changed; the hasChanged method will now return true.
      • clearChanged

        protected void clearChanged()
        Indicates that this object has no longer changed, or that it has already notified all of its observers of its most recent change, so that the hasChanged method will now return false. This method is called automatically by the notifyObservers methods.

        See Also:

        notifyObservers()notifyObservers(java.lang.Object)

      • hasChanged

        public boolean hasChanged()
        Tests if this object has changed.
        測試被觀察者對象是否有改變
        (相當於獲取當前被觀察者是否發生改變的狀態)

        Returns:

        true if and only if the setChanged method has been called more recently than the clearChanged method on this object;false otherwise.                                                               當且僅當對象調用setChanged方法是返回true,而不是在最近調用了clearChanged方法。其他情況返回false。

        See Also:

        clearChanged()setChanged()

      • countObservers

        public int countObservers()
        Returns the number of observers of this Observable object.     返回被觀察者的觀察者集中 觀察者的總數。

        Returns:

        the number of observers of this object.

 
 
 
 
 
 

 

如果被觀察者發生改變,就如通過調用hasChanged方法一樣
,然後通知所有的觀察者
,然後調用clearChanged方法讓該對象不再改變。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章