In a java swing project I have a model class that holds the state of a certain JPanel. I need to make this data available to the view. There are two options as I see it. Have a class that extends Observable and has the model as an instance variable. See the code example below. Or just have the model class extend Observable itself.
public class BoardObservable extends Observable {
private Board board;
public Board getBoardText() {
return board;
}
public void setBoardText(Board board) {
this.board = board;
setChanged();
notifyObservers(board);
}
}
So in the view class that implements Observer it will either use the Observable parameter or the Object parameter to populate the JPanel.
@Override
public void update(Observable o, Object arg) {
if(o instanceof BoardObservable) {
this.board = (Board) arg;
}
}
Which is the best option?