What-is-Observer-and-Observable-and-when-should-we-use-them?

1.) Observable is a Class and Observer is an Interface.

2.) Observable class maintain a list of observers.

3.) When an Observable object is updated, it invokes the update() method of each of its observers to notify that, it is changed.

You must be knowing about Observer Design Pattern. Java has provided utility classes to utilize this pattern.
package java.util;
public class Observable extends Object
This class represents an observable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have observed. An observable object can have one or more observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the Observable's notifyObservers method causes all of its observers to be notified of the change by a call to their update method. The order in which notifications will be delivered is unspecified. The default implementation provided in the Observable class will notify Observers in the order in which they registered interest, but subclasses may change this order, use no guaranteed order, deliver notifications on separate threads, or may guarantee that their subclass follows this order, as they choose. Note that this notification mechanism is has nothing to do with threads and is completely separate from the wait and notify mechanism of class Object. When an observable object is newly created, its set of observers is empty. Two observers are considered the same if and only if the equals method returns true for them. Constructor: Observable() Construct an Observable with zero Observers. Methods: void addObserver(Observer o): Adds an observer to the set of observers for this object, provided that it is not the same as some observer already in the set. protected void clearChanged(): Indicates that this object has no longer changed, or that it has already notified all of its observers of its most recent change, so that the hasChanged method will now return false. int countObservers(): Returns the number of observers of this Observable object. void deleteObserver(Observer o): Deletes an observer from the set of observers of this object. void deleteObservers(): Clears the observer list so that this object no longer has any observers. boolean hasChanged(): Tests if this object has changed. void notifyObservers(): If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed. void notifyObservers(Object arg): If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed. protected void setChanged(): Marks this Observable object as having been changed; the hasChanged method will now return true. package java.util; public interface Observer A class can implement the Observer interface when it wants to be informed of changes in observable objects. Method: void update(Observable o, Object arg): This
method is called whenever the observed object is changed.

Example:
package com.learnjavaj2ee;
import java.util.Observable;

/**
* Object of this class would be observed by observes registered with it.
* @author Arun.Singh
*
*/
public class MessageBoard extends Observable {
private String message;

public MessageBoard(String msg)
{
this.message = msg;
}

public String getMessage() {
return message;
}

public void changeMessage(String message) {
this.message = message;
setChanged();
notifyObservers(message);
}
}
package com.learnjavaj2ee;

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

/**
* Objects of this class would be observers
* @author Arun.Singh
*
*/
class Student implements Observer {
public void update(Observable o, Object arg) {
System.out.println("Message board changed: " + arg);
}
}
package com.learnjavaj2ee;

/**
* Demo program to show implementation of Observable and Observer
* @author Arun.Singh
*
*/
public class ObservableObserverDemo {
public static void main(String [] args)
{
MessageBoard msgBoard = new MessageBoard("First Message");

Student student1 = new Student();// This is first observer
Student student2 = new Student();// This is second observer

msgBoard.addObserver(student1);// Registering first observer with msgBoard object
msgBoard.addObserver(student2);// Registering second observer with msgBoard object

// Above both observers would be notified
msgBoard.changeMessage("Changed Message"); 
}
}
Share on Google Plus

About Admin

Arun is a JAVA/J2EE developer and passionate about coding and managing technical team.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment