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 TestsAn object under observation must adhere to two basic rules:
Before update(), the observed object must call both the setChanged() and notifyObservers() methods.
Declaration of the Observable class.
The declaration for java.util.Observable class is as follows:
public class Observable extends Object
Constructor of Observable Class
Given below is the constructor of the observable class:
Given below are the methods of the observable class:
Within a program, the interaction between an observable and an observer usually takes the form of the following sequence of events.
Given below are the examples of Observable in Java:
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); } }
Output:
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 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.
The above is the detailed content of Observable in Java. For more information, please follow other related articles on the PHP Chinese website!