Home > Java > javaTutorial > Observable in Java

Observable in Java

WBOY
Release: 2024-08-30 15:14:47
Original
592 people have browsed it

Observable is a class in the Java programming language that allows you to construct subclasses that other sections of the program can observe. Observing classes are informed when an object of this subclass changes. When an observer is informed of a change, the update() method is called. The Observable class is available in java.util package. A subclass of the class may be used to describe an object that the application needs to observe, and also, there may be one or more observers on an observable object. The observing class should implement the Observer interface, which specifies the update() method, which the observing classes must implement.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

An object under observation must adhere to two basic rules:

  • First, it must call the setChanged() method if it is changed.
  • It must call the notifyObservers() method when it is ready to notify observers of the update. The update() method in the observing object(s) is called as a result of this.

Before update(), the observed object must call both the setChanged() and notifyObservers() methods.

Syntax of Observable in Java

Declaration of the Observable class.

The declaration for java.util.Observable class is as follows:

public class Observable extends Object
Copy after login

Constructor of Observable Class

Given below is the constructor of the observable class:

  • Observable(): This creates an Observable that has no observers.

Methods of Observable Class

Given below are the methods of the observable class:

  • void addObserver(Observer o): This method creates a new observer to the collection of observers for such an object, as long as it isn’t the same as one that already exists.
  • protected void clearChanged(): This method means that this object has not changed or that it has already informed all of its observers of the most recent update, in which case the hasChanged() method returns false.
  • int countObservers(): The number of observers for this Observable object is returned by this method.
  • void deleteObserver(Observer o): This method removes an observer from this object’s list of observers.
  • void deleteObservers(): This method clears the observer list, removing all observers from this object.
  • boolean hasChanged(): This method determines whether or not this object has been modified.
  • void notifyObservers(): If the hasChanged () method indicates that this object has changed, alert all of its observers and then call the clearChanged( ) method to show that it has not changed. To update() method, a null is passed as the second parameter.
  • void notifyObservers(Object arg): If the hasChanged () method indicates that this object has changed, alert all of its observers and then call the clearChanged( ) method to show that it has not changed. To update() method, an object is passed as a second parameter.
  • protected void setChanged(): It indicates that this Observable object has been modified, and the hasChanged() method will now return true.

Working of Observable in Java

Within a program, the interaction between an observable and an observer usually takes the form of the following sequence of events.

  • When the public access method modifies the private data, changes the internal state, and calls the setChanged() method to show that the model’s state has changed. Then it calls notifyObservers() to let the observers know that something has changed. The call to notifyObservers() may be made from anywhere, such as in a separate thread’s update loop.
  • Next, each observer’s update() method is called, indicating that a state update has occurred.

Examples of Observable in Java

Given below are the examples of Observable in Java:

Example #1

Example for Observable in Java to perform changes with or without setChanged() 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_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);
}
}
Copy after login

Output:

Observable in Java

As in the above program, the Observable user-defined class ObservableEx is created by extending the Observable class, and also the Observer user-defined class ObserverEx is created by implementing the Observer interface where the class provided the implementation for the update() method. Next, the class ObservableEx contains two methods change_with_setChanged() and 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);
}
}
Copy after login

Output:

Observable in 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.

The above is the detailed content of Observable in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template