Abstract Interface Observer in Java

Abstract interface Observer in Java

The Observer interface is used to implement the view 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.

The Observer interface defines the update() method required for an object to observe subclasses of Observable. An Observer registers interest in an Observable() by calling the Observable.addObserver() method.

Observer objects that have been registered in this way will have their update() method invoked by the Observable when that object has changed.

The structure of the interface Observer is given as

public abstract interface java.util.Observer{
// Public Instance Methods
public abstract void update(Observable o, Object arg);

The details of the class structure are given as follows:

public abstract void update(Observable o, Object arg);

public abstract void update(Observable o, Object arg) method is called to indicate that the data in the model implemented by the specified Observable object has been modified. The arg parameter is used to communicate changed information from the model to its view.
This “hint” object can be used to efficiently update the views of a model. For example, an Observable object could represent satellite image data.

See also  Class DecimalFormat in Java

A set of Observer objects would provide different graphical views of the data. If the model data changed, the arg object would describe the part of the data that changed, and the Observer views could use this “hint” to update only parts of their displays.

o – The object that has been changed.
arg – A “hint” object that describes the change.

Share and Enjoy !

Leave a Reply

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