首頁  >  文章  >  Java  >  Java 中的可觀察性

Java 中的可觀察性

WBOY
WBOY原創
2024-08-30 15:14:47541瀏覽

Observable 是 Java 程式語言中的一個類,它允許您建構程式其他部分可以觀察到的子類別。當該子類別的物件發生變更時,觀察類別會收到通知。當觀察者被告知發生變更時,將呼叫 update() 方法。 Observable 類別在 java.util 套件中可用。該類別的子類別可以用來描述應用程式需要觀察的對象,並且在一個可觀察對像上可能有一個或多個觀察者。觀察類別應該實作 Observer 接口,該接口指定了觀察類別必須實作的 update() 方法。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

被觀察的對象必須遵守兩個基本規則:

  • 首先,如果發生更改,必須呼叫 setChanged() 方法。
  • 當它準備好通知觀察者更新時,必須呼叫notifyObservers()方法。因此,觀察對像中的 update() 方法被呼叫。

在update()之前,被觀察物件必須同時呼叫setChanged()和notifyObservers()方法。

Java 中 Observable 的語法

Observable 類別的宣告。

java.util.Observable 類別的陳述如下:

public class Observable extends Object

Observable 類別的建構子

下面給出的是可觀察類別的建構子:

  • Observable(): 這將會建立一個沒有觀察者的 Observable。

Observable 類別的方法

下面給出的是可觀察類別的方法:

  • void addObserver(Observer o): 此方法會為此類物件的觀察者集合建立一個新的觀察者,只要它與已存在的觀察者不同。
  • protected void clearChanged(): 這個方法意味著這個物件沒有改變或它已經通知了它的所有觀察者最近的更新,在這種情況下hasChanged() 方法傳回false .
  • int countObservers():此方法傳回此 Observable 物件的觀察者數量。
  • void deleteObserver(Observer o): 此方法從該物件的觀察者清單中刪除一個觀察者。
  • void deleteObservers(): 此方法清除觀察者列表,從該物件中刪除所有觀察者。
  • boolean hasChanged():此方法判斷此物件是否已被修改。
  • void notificationObservers(): 如果 hasChanged() 方法表明該物件已更改,則警告其所有觀察者,然後呼叫clearChanged() 方法以表示該物件未更改。對於 update() 方法,將 null 作為第二個參數傳遞。
  • void notificationObservers(Object arg): 如果 hasChanged() 方法表明該物件已更改,則警告其所有觀察者,然後呼叫clearChanged() 方法以表示該物件未更改。對於 update() 方法,一個物件作為第二個參數傳遞。
  • protected void setChanged(): 表示此 Observable 物件已被修改,hasChanged() 方法現在將傳回 true。

Java 中 Observable 的工作

在程式中,可觀察者和觀察者之間的互動通常採用以下事件序列的形式。

  • 當公共存取方法修改私有數據,改變內部狀態,呼叫setChanged()方法表示模型的狀態改變了。然後它呼叫notifyObservers()讓觀察者知道某些事情發生了變化。對notifyObservers()的呼叫可以從任何地方進行,例如在單獨執行緒的更新循環中。
  • 接下來,每個觀察者的 update() 方法都會被調用,表示發生了狀態更新。

Java 中的 Observable 範例

下面給出了 Java 中 Observable 的範例:

範例#1

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);
}
}

輸出:

Java 中的可觀察性

如上面的程序,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 #2

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:

Java 中的可觀察性

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.

Conclusion

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java ATM 程式下一篇:Java ATM 程式