ホームページ  >  記事  >  Java  >  Javaで観察可能

Javaで観察可能

WBOY
WBOYオリジナル
2024-08-30 15:14:47541ブラウズ

Observable は、プログラムの他のセクションが監視できるサブクラスを構築できる Java プログラミング言語のクラスです。このサブクラスのオブジェクトが変更されると、監視クラスに通知されます。オブザーバーに変更が通知されると、update() メソッドが呼び出されます。 Observable クラスは java.util パッケージで使用できます。クラスのサブクラスは、アプリケーションが監視する必要があるオブジェクトを記述するために使用できます。また、監視可能なオブジェクトには 1 つ以上のオブザーバーが存在する場合もあります。監視クラスは、監視クラスが実装する必要がある update() メソッドを指定する Observer インターフェイスを実装する必要があります。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

観察中のオブジェクトは 2 つの基本的なルールに従う必要があります:

  • まず、変更された場合は setChanged() メソッドを呼び出す必要があります。
  • オブザーバーに更新を通知する準備ができたら、notifyObservers() メソッドを呼び出す必要があります。この結果として、監視オブジェクトの update() メソッドが呼び出されます。

update() の前に、監視対象のオブジェクトは setChanged() メソッドとnotifyObservers() メソッドの両方を呼び出す必要があります。

Java の Observable の構文

Observable クラスの宣言。

java.util.Observable クラスの宣言は次のとおりです。

public class Observable extends Object

Observable クラスのコンストラクター

以下に、監視可能なクラスのコンストラクターを示します。

  • Observable(): これにより、オブザーバーのない Observable が作成されます。

観察可能なクラスのメソッド

監視可能なクラスのメソッドを以下に示します。

  • void addObserver(Observer o): このメソッドは、既存のオブザーバーと同じでない限り、そのようなオブジェクトのオブザーバーのコレクションに新しいオブザーバーを作成します。
  • protected void clearChanged(): このメソッドは、このオブジェクトが変更されていないか、最新の更新をすべてのオブザーバーにすでに通知していることを意味します。この場合、hasChanged() メソッドは false を返します。 .
  • int countObservers(): この Observable オブジェクトのオブザーバーの数は、このメソッドによって返されます。
  • void deleteObserver(Observer o): このメソッドは、このオブジェクトのオブザーバーのリストからオブザーバーを削除します。
  • void deleteObservers(): このメソッドはオブザーバー リストをクリアし、このオブジェクトからすべてのオブザーバーを削除します。
  • boolean hasChanged(): このメソッドは、このオブジェクトが変更されているかどうかを判断します。
  • void NoticeObservers(): hasChanged() メソッドがこのオブジェクトが変更されたことを示した場合、すべてのオブザーバーに警告し、clearChanged() メソッドを呼び出してオブジェクトが変更されていないことを示します。 update() メソッドには、2 番目のパラメータとして null が渡されます。
  • void NoticeObservers(Object arg): hasChanged() メソッドがこのオブジェクトが変更されたことを示している場合、すべてのオブザーバーに警告し、clearChanged() メソッドを呼び出してオブジェクトが変更されていないことを示します。 update() メソッドには、オブジェクトが 2 番目のパラメータとして渡されます。
  • protected void setChanged(): これは、この Observable オブジェクトが変更されたことを示し、hasChanged() メソッドは true を返すようになります。

Java での Observable の仕組み

プログラム内では、オブザーバブルとオブザーバーの間の対話は通常、次の一連のイベントの形式を取ります。

  • パブリック アクセス メソッドがプライベート データを変更し、内部状態を変更し、setChanged() メソッドを呼び出してモデルの状態が変更されたことを示すとき。次に、notifyObservers() を呼び出して、何かが変更されたことをオブザーバーに知らせます。 NoticeObservers() への呼び出しは、別のスレッドの更新ループなど、どこからでも行うことができます。
  • 次に、各オブザーバーの update() メソッドが呼び出され、状態の更新が発生したことが示されます。

Java での Observable の例

Java での Observable の例を以下に示します。

例 #1

setChanged() メソッドを使用または使用せずに変更を実行する Java の Observable の例。

コード:

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() の 2 つのメソッドが含まれています。

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。