Class Observable in Java

Class Observable in Java

Class Observable in java represents an Observable object or data in the model view framework, therefore it is the superclass of all observable objects. This class can be subclassed to represent an object that the application wants to have observed.

Subclasses of the Observable class are used to implement the model portion of the model-view paradigm. The idea is that an Observable object, the model, represents some data that is being manipulated through a user interface, while Observer objects provide the user with a view of the data. When the Observable object is modified, it tells the Observer objects that the model has been modified by calling notifyObservers(). An Observer object registers with an Observable object to receive notifications when the Observable is modified. The Observer object is then notified of changes via the update() method.

Ads code goes here

Basically an Observable object can have one or more observers. After an Observable instance changes, an application calling the observable’s notify observer’s method.

Causing all of its observers to be notified of the changes by a call to their update method. It maintains an internal flag called “changed.”To set and clear which may be queried by has Changed by another observer.

The Observable and Observer objects play an important role in implementing Model-View-Controller architecture in Java.

They are typically used in a system where one object needs to notify another about the occurrences of some important changes.

READ  Class ChoiceFormat in Java With Example

Observable is a class and Observer is an interface. Hence they are found in the java.util package as a part of the Java Core Utility Framework. However, the Observable entity found in the JavaFX is an interface and is a part of the javaf.beans package.

The basic idea is the same, yet the implementation varies.

This article shall try to explain the concepts behind the paradigm and how they are used in Java code

The class Observable is defined as below:

 

public class java.util.Observable extends java.lang.Object{
//constructor
public Observable();
//Methods
public synchronized void addObserver(Observer observer);
public synchronized void deleteObserver(Observer observer);
public synchronized void deleteObservers();
public void notifyObservers();
public void notifyObserver(Observer obj);
public synchronized boolean hasChanged();
public synchronized int countObservers();
protected synchronized void clearChanged();
protected synchronized void setChanged();
} 

The details of the class structure are given as follows:

public Observable();

public Observable() constructor creates an Observable object with no registered Observer objects.

public synchronized void addObserver(Observer o);

public synchronized void addObserver(Observer o) method registers the given Observer with this Observable object. The given Observer is then notified when notifyObservers() is called.

Parameter
o – The Observer to be added.

public synchronized int countObservers();

public synchronized int countObservers() method returns the number of Observer objects that are registered with this Observable object.

This method returns the number of registered Observer objects for this Observable object.

public synchronized void deleteObserver(Observer o);

public synchronized void deleteObserver(Observer o) method unregisters the given Observer with this Observable object. The given Observer is no longer notified when notifyObservers() is called.

Parameter
o – The Observer to be removed.

public synchronized void deleteObservers();

public synchronized void deleteObservers() method unregisters all of the Observer objects of this Observable object. Thus, no objects are notified if notifyObservers() is called.

public synchronized boolean hasChanged();

public synchronized boolean hasChanged() method returns the value of an internal “dirty” flag. The flag can be modified using the protected methods setChanged() and clearChanged().

READ  Class FileWriter in Java

This method returns true if this object has been flagged as changed; false otherwise.

public void notifyObservers();

public void notifyObservers() method calls the update() method of all registered Observer objects. The value passed
as the second argument to each of the update() method calls is null.

public void notifyObservers(Object arg);

public void notifyObservers(Object arg) method calls the update() method of all registered Observer objects. The value passed as the second argument to each of the update() method calls is the given object arg.

This “hint” object can be used to efficiently update the views of a model. For example, an observable object could represent satellite image data. A set of Observer objects would
provide different graphical views of the data. If the model data changes, the arg object describes the part of the data that changed, and the Observer views could use this “hint” to update only parts of their displays.

Parameter
arg – A “hint” object that describes a change.

protected synchronized void clearChanged();

protected synchronized void clearChanged() method sets an internal “dirty” flag to false. After this method is called, this object hasChanged() method returns false.

protected synchronized void setChanged();

protected synchronized void setChanged() method sets an internal “dirty” flag to true. After this method is called, this object’s hasChanged() method returns true.

Apart from these Observable class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *