Observable 是 Java 程式語言中的一個類,它允許您建構程式其他部分可以觀察到的子類別。當該子類別的物件發生變更時,觀察類別會收到通知。當觀察者被告知發生變更時,將呼叫 update() 方法。 Observable 類別在 java.util 套件中可用。該類別的子類別可以用來描述應用程式需要觀察的對象,並且在一個可觀察對像上可能有一個或多個觀察者。觀察類別應該實作 Observer 接口,該接口指定了觀察類別必須實作的 update() 方法。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗被觀察的對象必須遵守兩個基本規則:
在update()之前,被觀察物件必須同時呼叫setChanged()和notifyObservers()方法。
Observable 類別的宣告。
java.util.Observable 類別的陳述如下:
public class Observable extends Object
Observable 類別的建構子
下面給出的是可觀察類別的建構子:
下面給出的是可觀察類別的方法:
在程式中,可觀察者和觀察者之間的互動通常採用以下事件序列的形式。
下面給出了 Java 中 Observable 的範例:
Java 中 Observable 所使用或不使用 setChanged() 方法執行變更的範例。
代碼:
import java.util.*; // This is the observer class class ObserverEx implements Observer { public void update(Observable obj, Object arg) { System.out.println("Update in an observer side."); } } // This is the obsrvable class class ObservableEx extends Observable { void change_with_setChanged() { setChanged(); System.out.println("Change the status with setChanged : " + hasChanged()); notifyObservers(); } void change_without_setChanged() { System.out.println("Change status with setChanged : " + hasChanged()); notifyObservers(); } } public class HelloWorld { public static void main(String args[]) { ObservableEx Observable = new ObservableEx(); ObserverEx observer1 = new ObserverEx(); ObserverEx observer2 = new ObserverEx(); Observable.addObserver(observer1); Observable.addObserver(observer2); Observable.change_with_setChanged(); Observable.change_without_setChanged(); int no = Observable.countObservers(); System.out.println("The number of observers for this Observable are : " + no); } }
輸出:
如上面的程序,Observable用戶定義類別ObservableEx是透過擴展Observable類別創建的,Observer用戶定義類別ObserverEx是透過實作Observer介面創建的,該類別提供了更新的實作( ) 方法。接下來,ObservableEx類別包含兩個方法change_with_setChanged()和change_without_setChanged()。
The method change_with_setChanged() call the setChanged() and then notify all the observer, which means the changes done here with setChanged will be notified to all the observer.Whereas the method change_without_setChanged() does not call the setChanged() and notify all the observers, which means the changes done here without setChanged will not show to all the observer.
Then, in the main function, one Observable and two Observer objects are created, and also add both the Observer object to this Observable. Next, on Observer objects, the change_with_setChanged() method is called, which notifies both the observers and called the update() method, which prints the message, whereas the change_without_setChanged() method does not call the update() method of the observers. And next finding and printing the number of observers, as we can see in the above output.
Example for Observable in Java to perform changes with or without clearChanged() method.
Code:
import java.util.*; // This is the observer class class ObserverEx implements Observer { public void update(Observable obj, Object arg) { System.out.println("Update in an observer side."); } } // This is the obsrvable class class ObservableEx extends Observable { void change_with_clearChanged() { setChanged(); System.out.println("Removes all the changes made by setChanged method."); // clearChanged method clearChanged(); notifyObservers(); } void change_without_clearChanged() { setChanged(); System.out.println("Does not removes all the changes made by setChanged method. "); notifyObservers(); } } public class HelloWorld { public static void main(String args[]) { ObservableEx Observable = new ObservableEx(); ObserverEx observer1 = new ObserverEx(); ObserverEx observer2 = new ObserverEx(); Observable.addObserver(observer1); Observable.addObserver(observer2); Observable.change_with_clearChanged(); Observable.change_without_clearChanged(); int no = Observable.countObservers(); System.out.println("The number of observers for this Observable are : " + no); Observable.deleteObserver(observer2); no = Observable.countObservers(); System.out.println("The number of observers after delete for this Observable are : " + no); } }
Output:
As in the above program, the classes ObservableEx and ObserverEx are created. Next, the class ObservableEx contains two methods change_with_clearChanged() and change_without_clearChanged(). The method change_with_clearChanged() call the setChanged(), clearChanged() which removes all the changes made by setChanged method. Whereas the method change_without_clearChanged() does not call the clearChanged() which means the changes made by setChanged method will not remove.
Then, in the main function, one Observable and two Observer objects are created, and also add both the Observer object to this Observable. Next, on Observer objects, the change_with_clearChanged() method is called, which does not call the update() method of the observers, whereas the change_without_setChanged() method calls the update() method of the observers. And next, delete the observer1 and finding reaming Observer and printing, as we can see in the above output.
The Observable class is available in java.util package. An Observable is a class in Java that allows the creation of an Observable subclass that other sections of the program can observe.
以上是Java 中的可觀察性的詳細內容。更多資訊請關注PHP中文網其他相關文章!