开发者

How do I make a Class extend Observable when it has extended another class too?

I am learning J开发者_JAVA百科ava and I want to make my class into an observable class.

However I already have it extending another class.

What should I do?


I'd recommend avoiding using the Observable class altogether, but rather define event-specific listeners and corresponding event definitions. Then define a list of listeners within your class along with methods to add and remove listeners, and propagate events to them (see below).

Observable forces you to use java.lang.Object to represent events and then check the event type using instanceof, which is an ugly non-OO approach, and makes the code more difficult to understand. If you look at the classes within the javax.swing package you'll see they avoided using Observer / Observable altogether and used an approach similar to the below.

Event Definition

public class MyChangeEvent extends EventObject {
  // This event definition is stateless but you could always
  // add other information here.
  public MyChangeEvent(Object source) {
    super(source);
  }
}

Listener Definition

public interface MyChangeListener {
  public void changeEventReceived(MyChangeEvent evt);
}

Class Definition

public class MyClass {
  // Use CopyOnWriteArrayList to avoid ConcurrentModificationExceptions if a
  // listener attempts to remove itself during event notification.
  private final CopyOnWriteArrayList<MyChangeListener> listeners;

  public class MyClass() {
    this.listeners = new CopyOnWriteArrayList<MyChangeListener>();
  }

  public void addMyChangeListener(MyChangeListener l) {
    this.listeners.add(l);
  }

  public void removeMyChangeListener(MyChangeListener l) {
    this.listeners.remove(l);
  }

  // Event firing method.  Called internally by other class methods.
  protected void fireChangeEvent() {
    MyChangeEvent evt = new MyChangeEvent(this);

    for (MyChangeListener l : listeners) {
      l.changeEventReceived(evt);
    }
  }
}


Java doesn't allow multiple inheritance, so there is no direct way to do it. You should consider using a delegate pattern having your main object that delegates his observer behaviour to an another object..

class YourObject extends ItsAncestorClass
{
      private Observer includedObserver;

      public Observer getHisObserver(..)
}

Another approach would be turning the object from which your class is extending to an interface, then you'll be allowed to extend from Observer.


Another option is to wrap your object in an Observable object.

public class MyObjectObservableWrapper implements Observable {  
  private MyObject myObject;
  public MyObjectObservaleWrapper(MyObject myObject){
    this.myObject = myObject;
  }
  // interface methods here
}

This option works when the data to be used by the Observable methods is accessible through public methods of MyObject. So, it may not be suitable for all cases.


Multiple inheritance (to extend two classes) is not possible in Java. Is considered a bad design in almost all cases.

If you give us some more information, maybe somebody can help you a little more.


You can extend Observable and wrap the original parent in your to-be observable child class. Delegate all method calls to the wrapped object.


Use a bridge.

Create a class that extends Observable that the first class just calls the methods of the second class.

Bridge method detail:

 public class XXX {
    public class XXXObservableBridge : Observable {
       public void RaiseEvent();
       // Listeners etc
    }

    private XXXObservableBridge ObservableBridge;

    XXX() {
       ObservableBridge = new ObservableBridge;
    }

    public Observable AsObservable() { return ObservableBidge; }

    public void RaiseEvent() { ObservableBridge.RaiseEvent(); }
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜