Observable Class

https://www.tutorialspoint.com/java/util/java_util_observable.htm

  • 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.

Methods of 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.

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.

Example #1

Example for Observable in Java to perform changes with or without setChanged() method.

public class Main {
    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);
    }
}

ObservableEx.java

import java.util.Observable;

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

ObserverEx.java

import java.util.Observable;
import java.util.Observer;

public class ObserverEx implements Observer {
    public void update(Observable obj, Object arg) {
        System.out.println("Update in an observer side.");
    }
}

Output

Change the status with setChanged : true
Update in an observer side.
Update in an observer side.
Change status with setChanged : false
The number of observers for this Observable are : 2

Example #2

Example for Observable in Java to perform changes with or without clearChanged() method.

public class Main {
    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);
    }
}

ObservableEx.java

import java.util.*;

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

ObserverEx.java

import java.util.*;

// This is the observer class
public class ObserverEx implements Observer {
    public void update(Observable obj, Object arg) {
        System.out.println("Update in an observer side.");
    }
}

Output

Removes all the changes made by setChanged method.
Does not removes all the changes made by setChanged method. 
Update in an observer side.
Update in an observer side.
The number of observers for this Observable are : 2
The number of observers after delete for this Observable are : 1