Search code examples
javaswinglistenerpropertychangelistener

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


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());
}

Solution

  • 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();
    }