capovawi
capovawi

Reputation: 375

How to notify changes when implementing MVC-based GUI

So far, I have implemented a GUI based on Swing, and according to the MVC pattern like (here), where events are fired from both view (by using JComponents capabilities) and model (by using PropertyChangeSupport bean). The controller is in the middle, listening both of them and forwarding events, as follows:

VIEW

public class GUIview extends JFrame {
    public void propertyChange(final PropertyChangeEvent event) {
        if (event.getPropertyName().equals(GUIcontroller.A1_PROPERTY)) {
            method_a1(event.getNewValue());
        } else if (event.getPropertyName().equals(GUIcontroller.A2_PROPERTY)) {
            method_a2(event.getNewValue());
        }
    }
    public void method_a1() {...}
    public void method_a2() {...}
}

CONTROLLER

public class GUIcontroller implements PropertyChangeListener {

    public static final String A1_PROPERTY = "a1";
    public static final String A2_PROPERTY = "a2";
    public static final String B1_PROPERTY = "b1";
    public static final String B2_PROPERTY = "b2";

    public void propertyChange(PropertyChangeEvent event) {
        if (event.getPropertyName().charAt(0) == 'a') {
            GUIview.propertyChange(event);
        } else if (event.getPropertyName().charAt(0) == 'b') {
            GUImodel.propertyChange(event);
        }
    }
}

MODEL

public class GUImodel {

    public PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
    protected void firePropertyChange(String propertyName, Object oldValue,
            Object newValue) {
        propertyChangeSupport.firePropertyChange(propertyName, oldValue,
                newValue);
    }   
    public void propertyChange(final PropertyChangeEvent event) {

        if (event.getPropertyName().equals(GUIcontroller.B1_PROPERTY)) {
            method_b1(event.getNewValue());
        } else if (event.getPropertyName().equals(GUIcontroller.B2_PROPERTY)) {
            method_b2(event.getNewValue());
        }
    }
    public void method_b1() {...}
    public void method_b2() {...}
}

Now it works, but as far as I know (or as far as I have read), the view should only contain layout functionality, and all the work must be done by the controller and the model. On the other hand, the controller should be as thinner as possible.

I can´t find a reason to implement propertyChange() methods on both view and model, and make method calls from there, instead of directly call those methods from the controller, like this:

public class GUIcontroller implements PropertyChangeListener {

    public static final String A1_PROPERTY = "a1";
    public static final String A2_PROPERTY = "a2";
    public static final String B1_PROPERTY = "b1";
    public static final String B2_PROPERTY = "b2";

    public void propertyChange(PropertyChangeEvent event) {
        if (event.getPropertyName().equals(GUIcontroller.A1_PROPERTY)) {
            GUIview.method_a1(event.getNewValue());
        } else if (event.getPropertyName().equals(GUIcontroller.A2_PROPERTY)) {
            GUIview.method_a2(event.getNewValue());
        } else if (event.getPropertyName().equals(GUIcontroller.B1_PROPERTY)) {
            GUImodel.method_b1(event.getNewValue());
        } else if (event.getPropertyName().equals(GUIcontroller.B2_PROPERTY)) {
            GUImodel.method_b2(event.getNewValue());
        }
    }
}

Considering these two approaches, which one is more tight to a real MVC pattern?

Which could be the main reason to implement propertyChange() methods on view and model?

Upvotes: 0

Views: 3869

Answers (1)

Guillaume Polet
Guillaume Polet

Reputation: 47607

In MVC, Views listen to the model and update themselves accordingly. Controllers are reponsible to handle user input to modify the model. Model is responsible to fire appropriate events when it changes.

Which could be the main reason to implement propertyChange() methods on view and model?

  • On view: fine, upon property change events, the view should update itself accordingly
  • On model: no reason to implement that method, since the model will never listen for property changes.

Upvotes: 4

Related Questions