Creating a subclass observable in java

I have a Passenger subclass that extends from the Person class. I want to make the Passenger class an observable object. So I do it this way

class Person extends Observable{    
}

class Passenger extends Person{
    public void move(){
        setChanged();
        notifyObservers();
    }   
}

class Manager implements Observer{
    @Override
    public void update(Observable o, Object arg) {
    }  
}

Is it wrong to use the Person Observable class if I don't need observable functions for this class? If so, how can I change this?

+4
source share
2 answers

I think the Obervable class in java is a pretty poorly thought out class. He really wants to be in the interface, in my opinion.

However, instead of the Person extending from the Observed, you can use the composition at the Passenger level.

"" :

private Observable observable = new Observable();

public void addObserver(Observer o) {
    observable.addObserver(o);
}

public void notifyObservers() {
    observable.notifyObservers();
}

. .

:

java.util.Observable ?

+4

Person , Passenger Observable Person. .

0

Source: https://habr.com/ru/post/1618437/


All Articles