Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observable in Java

I'm trying to understand the Observer and the Observable.

Here's an example that I'm trying to figure out:

public class IntegerDataBag extends Observable implements Iterable<Integer> {

    private ArrayList<Integer> list= new ArrayList<Integer>();

    public void add(Integer i){
        list.add(i);
        setChanged();
        notifyObservers();
    }

    public Iterator<Integer> iterator(){
        return list.iterator();
    }

    public Integer remove (int index){
        if (index< list.size()){
            Integer i = list.remove(index);
            setChanged();
            notifyObservers();
            return i;
        }
        return null;
    }

}

public class IntegerAdder implements Observer {

    private IntegerDataBag bag;

    public IntegerAdder(IntegerDataBag bag) {
        this.bag = bag;
        bag.addObserver(this);
    }

    public void update(Observable o, Object arg) {
        if (o == bag) {
            System.out.println("The contents of the IntegerDataBag have changed");
        }
    }

}
  1. The bag.addObserver() can be made only because IntegerDataBag extends Observable?

  2. Where is this observer being add to? What is being created and where?

  3. What is the difference between setChanged() and notifyObservers()?

  4. I don't understand the update method; what does arg stand for? Why do I need to check that o==bag? Why would I update another observable?

  5. Why should I need this observer anyway?

like image 284
Numerator Avatar asked Sep 01 '11 13:09

Numerator


People also ask

What is Observer and Observable in Java?

The Java language supports the MVC architecture with two classes: Observer : Any object that wishes to be notified when the state of another object changes. Observable : Any object whose state may be of interest, and in whom another object may register an interest.

What replaced Observable in Java?

You can use PropertyChangeEvent and PropertyChangeListener from java. beans package. PropertyChangeListener replaces Observer , but what should I extend/implement in place of Observable ?

How does Observer work in Java?

Observer is a behavioral design pattern that allows some objects to notify other objects about changes in their state. The Observer pattern provides a way to subscribe and unsubscribe to and from these events for any object that implements a subscriber interface.

Why Observable is deprecated in Java?

Class Observable. Deprecated. This class and the Observer interface have been deprecated. The event model supported by Observer and Observable is quite limited, the order of notifications delivered by Observable is unspecified, and state changes are not in one-for-one correspondence with notifications.


1 Answers

  1. Yes. addObserver is a method in the Observable abstract class. See Observable in the Java documentation.
  2. It is added to a list in Observable.
  3. A call to notifyObservers will do nothing until setChanged is set.
  4. You might have multiple Observables in the same application.
  5. Observer is a common design pattern. The usual example is when you have a Model and multiple Views. Each View is an Observer on the Model; if the Model changes, the Views get updated.
like image 102
S.L. Barth Avatar answered Sep 27 '22 22:09

S.L. Barth