Passing Model to View in MVC

I've always understood that MVC means the model doesn't need to know anything about the view and vice versa. However, in my university course, an example of a view using the MVC pattern is given as:

class View implements Updatable {

    private final JButton button = new JButton("Press Me!");
    private final JTextField textField = new JTextField(10);

    //some setup of the button and textfield

    public void update(Model model) {
        if (model.morePressesAllowed()) {
            textField.setText(String.valueOf(model.count()));
        } else {
            textField.setText("Too Many!");
            button.setEnabled(false);
        }
    }
}

      

It seems strange to me that the view needs to know what methods the model has. It looks like it would be better if the MVC pattern would render the button and textarea for the controller and have an update method on the controller?

The model just increments the number, and if it reaches 5, then more. DressesAllowed returns false.

Also the model has an Updatable list, and when the counter changes it, it cyclically updates and updates the calls, while this is better than having a Views list, does it still seem like the controller should be responsible for reporting when the model changes?

EDIT: model:

class Model {
    private final List<Updatable> views = new ArrayList<Updatable>();
    private int count;
    private boolean morePressesAllowed = true;
    public void addObserver(Updatable observer) {
        views.add(observer);
    }
    public void increment() {
        count++;
        if (count >= 5) {
            morePressesAllowed = false;
        }
        notifyObservers();
    }
    private void notifyObservers() {
        for (Updatable view : views) {
            view.update(this);
        }
    }
}

      

Controller / Main class: (also shouldn't the controller create a model and view and be a normal public class?)

public class GuiApp {
    private View view = new View(new Controller());
    private Model pressCounter = new Model();

    class Controller implements ActionListener {
        public void actionPerformed(ActionEvent actionEvent) {
            pressCounter.increment();
        }
    }
    GuiApp() {
        pressCounter.addObserver(view);
    }
}

      

Also I prefer it something like this: http://www.tutorialspoint.com/design_pattern/mvc_pattern.htm Because the fact that the controller just wraps a bunch of view methods and a model, which although MVC seems to be similar to what Model and View don't know anything about each other, does this seem less efficient and complex?

+3


source to share


1 answer


It seems like it would be better if the MVC pattern would render the button and textarea for the controller and have an update method on the controller?

This will create a link between the view and the controller. It is important that each layer knows as little as possible about each other. You don't want the controller to depend on some text boxes or buttons. You want the controller to pass data back to the view, and don't care what happens to it. This is the work of the gaze. This is the whole point of delegation.

Also the model has an Updatable list, and when the counter changes it, it cyclically updates and updates the calls, while this is better than having a Views list, does it still seem like the controller should be responsible for reporting when the model changes?

You're right. The role of the view is not to call a method on the Model. Again, this creates an unwanted connection.



also shouldn't the controller create a model and view and be a normal public class?

Yes. More often than not, I see that the controller interacts with the model layer or some service, which then returns the models, which are then delegated to the view.

Because the fact that the controller just wraps a bunch of View and Model methods, which while it seems more MVC like Model and View don't know anything about each other, does it seem less efficient and more complex?

These methods are known as proxy methods. Methods that simply delegate the call to another. This is useful when you are trying to maintain an appropriate separation of concerns in your architecture. It depends on your definition of the complex. Yes, it adds more methods to your controller. Again, when the next developer comes along, if he or she can make the assumption that your application strictly follows the MVC architecture, it will be much easier for them to develop time against your code, as opposed to having to work out micro-optimizations and work around.

0


source







All Articles