2

Since I learnt about MVC, I used it for every app I made (which is arguably not the best idea, but that's not the topic of this question). All of them small, 1000 LoC apps. I am using Java and Swing for the GUI.

What usually happens is this:

The view (the GUI class) reports to the controller about any GUI event (most commonly a button click) made. For example when a button is pressed, the view simply calls controller.someButtonPressed() or controller.someOtherButtonPressed(). It's only reaction to user input is reporting to the controller, nothing else.

This, I think, is fine and is proper MVC View implementation. The part I'm having doubts about is the following:

In the controller's someButtonPressed() methods, it very often simply delegates to the model.

For example:

public void someButtonPressed(){
    model.doTheAppropriateThing();
}

Nothing more. No 'decision making' or actual 'interpretation' of what the view reported. Very often, only simple delegation to the model.

Is it considered reasonable when implementing MVC structures and specifically controllers, to have the controller often simply delegate directly to the model in reaction to GUI events? Or does this signal that maybe I'm doing something wrong?

1

In an ideal world, your model code would be in a separate DLL / Shared Library / Jar file / re-usable module, and could be included in a system that handles API requests from any client, whether that be an Android App speaking a socket-level protocol or a web page speaking HTTP or some freaky combination of something else.

In those cases, IMHO, the Controller is responsible for making the data that belongs in the View (the app, the web page) appropriately transformed for the view, but also takes care of error handling (HTTP status codes perhaps in one case, and some custom format in another case).

Essentially controllers mediate between models and views, which means they might need logic that deals with that mediation (data transformation, creating DTOs, returned error information in the appropriate way). But the logic of what it means in the system when a user takes some action, is the responsibility of the model. The model is the view-and-controller-agnostic representation of the domain. The controller mediates between.

  • Thanks for answering. Not sure if I understand your answer, but I'm not sure it answers exactly the question. I know what the job of the controller is, and that it's supposed to mediate between the model and the view. My question was if it's reasonable that often the controller mediates simply by delegating directly to the appropriate method on the model, when the view reports about a user action. Do you have an opinion regarding this? – Aviv Cohn May 3 '14 at 18:12
  • In theory, it's possible. I don't know if it would be a good idea, but in theory it's possible. Edit: in theory it's not incorrect. If you didn't care about the return value of the function and various other things were already taken care of... but this is actually almost a code-smell to me, because it probably means that the code base suffers from low cohesion, and IMHO high cohesion is very important and perhaps more important than many other more popular principles. – RibaldEddie May 3 '14 at 19:41
  • I edited to point out that the model should be view AND controller agnostic. – RibaldEddie May 3 '14 at 20:46
  • I'll describe an example situation, please say if it seems to you like a correct MVC implementation: consider a word processor. The user clicks the Bold button on the GUI (the view) to make text bold. In reaction, the view calls controller.boldButtonClicked(). In the boldButtonClicked() method, the controller simply calls model.makeTextBold(). The model then carries out the logic needed to make text bold, and updates the view. Does this seem reasonable? The part where the controller simply delegates to the model? – Aviv Cohn May 3 '14 at 21:52
  • It's going to be much, more complex than that fundamentally in your example. For example in the context of a word processor, the Controller layer probably has to do things like find out which characters in the document are highlighted and let the model know that. – RibaldEddie May 3 '14 at 23:29
0

I think it's fine if you have methods like the someButtonPressed() in your example. It is important though, that you only have to change the controler but not the model if the logics when pressing someButton changes somewhen in the future. Also the other way around: if the models doTheAppropriateThing() method changes then you don't have to care for the view.

The result is that your view and model are decoupled and when the controller is only "passing" the method calls that's fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.