首页 > Java > java教程 > 正文

Java 中的可观察性

WBOY
发布: 2024-08-30 15:14:47
原创
486 人浏览过

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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!