firePropertyChange - does it matter what the parameters oldValue and newValue are?

915 views Asked by At

I'm currently working on a project in which a propertyChangeListener is used to update the view-part of the program when somthing has changed in the model-part. The program solves a given sudoku puzzle and updates the model to include the solution.

I use a firePropertyChange method which has the parameters oldValue and newValue. i understand that these have to be different in order for something to happen but does it actually matter what they are? In my program the model is always updated when something happens and I never use the information contained in oldValue or newValue.

Could it be possible to just put these as "1" and "2" to make sure they are always different? Would this cause any other issues. Here is a method called clear which resets the sudoku to a blank board:

public void clear() {
    String oldBoard = getBoard();
    for (int i=0; i<9; i++) {
        for (int k=0; k<9; k++)
            plan[i][k] = 0;
    }
    pcs.firePropertyChange("clear", oldBoard, getBoard());
}
2

There are 2 answers

0
Antony Ng On BEST ANSWER

It would work if the receiver of the property change is your internal classes and they have the knowledge of ignoring the old/new values of the property change event. You may still need to document this fact clearly in case you may forget it yourself after some time later. To make code more maintainable and readable however, it would be a better approach you write your own listener interface, though it would requires some more works.

An example:

public interface SudokuListener {
    void sudokuCleared();
}
0
Hovercraft Full Of Eels On

Yes, it matters in the general sense, since when you are writing the code for the observed entity, you write it with the idea that this entity can never know who or what is listening to it or how the observers are going to use the information that the listener is going to provide to them. It may be important to the observer, it may not, but always assume that it is, keep the information as accurate as possible, and your code will be safer for future extensions and use.