0

Here are my Observer and Observable interfaces

public interface Observable<T> {
    void addObserver(Observer<T> o);
    void removeObserver(Observer<T> o);
    void removeAllObservers();
    void notifyObservers();
}


public interface Observer<T> {
    public void update(Observable<T> o);
}

These work if I have two classes that know about each other, however, how do I handle a situation like below.

  • RootComposite (Creates a Word List)
  • WordListComposite (Creates a Word)
    • WordDialog

Now if RootCompoiste needs to know about changes in WordListComposite, I can implement it like that following:

public class RootComposite extends Composite implements Observer<WordListComposite > {

public RootComposite() {

WordListComposite wl = new WordListComposite();
wl.addObserver(this);

}

...

@Override
public void update(WordListComposite o) {
    this.lblMessage = o.getMessage();
}

...
}

This will work as expected.

But how does RootComposite receive updates from WordDialog?

What is the best approach in this case?

sblundy
  • 60,628
  • 22
  • 121
  • 123
jax
  • 37,735
  • 57
  • 182
  • 278

1 Answers1

0

WordListComposite could also implement the Observer interface and WordDialog the Observable interface. WordListComposite could observe WordDialog and when it receives a notification from WordDialog trigger a notification to RootComposite (who is watching WordListComposite). Basically you have an action fire, and then just send the notifications up the tree.

Jeremy Raymond
  • 5,817
  • 3
  • 31
  • 33
  • I tired this suggesting but an getting an error about implementing two generic Observers, I have opened a new thread here http://stackoverflow.com/questions/4282437/getting-error-for-generic-interface-the-interface-observer-cannot-be-implemente – jax Nov 26 '10 at 04:10
  • Is WordDialog a child of WordListComposite or also a child of RootComposite? In your description it looks like WordDialog is a child of WordListComposite. So WordDialog could notify WordListComposite who would then notify RootComposite. – Jeremy Raymond Nov 26 '10 at 14:49