by quietbritishjim · 7y ago · 0 comments
The distinction between these two parts is that the view is just a display of the data (model) whereas the controller is a way for the user to edit it. This was more relevant back in the 1970s where these things were more often separate. For a modern(ish) example, think of Vim's separation between the main display of your text file (the view) and its command line at the bottom (the controller). In most modern GUIs this distinction is meaningless, so you would group controller and view into the view and have a simple overall model-view separation.
Now I'm not saying that all of the classes you've called (something)Controller should be moved to the UI, I'm just saying that they're misnamed (at least by the traditional definitions). I think that your interpretation of model vs controller is raw model data vs non-graphical operations that act on them. But operations on the data are just as much part of the model as the data itself. For example, if I have a word processor then my model is a document, including operations on it. If I want to update the document programmatically (bypassing the GUI), for example to add a word, then I expect to call a method of the model. This should do everything to keep the model consistent, such as returning an updated word count. But that still doesn't need anything called a controller.
I just said that I'm not telling you to move your controller classes to the UI (view) layer. But that's a general comment about the meaning of "model view controller". The specific example you present on that page is "MenuModel" and "MenuController". These sound like things that both belong in the UI layer. Going back to the word processor example, your model might have a SaveToFile() method (although it would expect a file name parameter - popping up a save file dialogue box is the UI's job). But it won't know whether that came from a File->Save menu item or the user hitting ctrl+S. It's the UI's (view's) job to create a menu, choose what commands go on it, and map the OnClick events to actions on the model.
No doubt others will disagree with my understanding of the definition of "controller". This is the one thing you can be really sure about with that term! And it's another good reason to avoid it. Everyone agrees with "model" and "view" mean, so if you use those terms then people will know what you mean, but if you describe something as a "controller" then it just causes confusion.
There's some really good ideas in it, and you definitely make valid points. But this ^ happens too much for "MVC" to be a valuable term.
Over the years I've extracted many components from my controllers to the point where they're finally getting pretty slim. At first I let the controller fetch the model - e.g. update a model from the network, but later extracted this into a manager/provider object so multiple controllers and views can easily share this. Then I removed anything resembling business logic from the controller as I realised that was part of the model too, and moved this into service objects or utility functions (and not on the model-objects themselves, as I try to keep these focused just on data storage and serialization/deserialization). This might also include some form of dispatch if the business logic is triggered by a command. Similarly I moved all kinds of formatting code into presenter objects. Then I added viewmodels as an adapter layer on top of the actual model - both to insulate the controller from the actual model (didn't seem that important at first, but I finally 'got it' later) and to have a place to store local state such as current selection, scroll position, etc. And finally, I recently went all the way with the viewmodel after I extracted the logic of which view leads to which view out of the controller and into a flow coordinator object. This way, a given controller doesn't know what view is shown when you click the 'Next' button - instead it asks its viewmodel. That way the controller doesn't know anything about what needs to happen next or how the next view is shown. And that way all the logic about which view leads to which view resides in the flow coordinator which injects this into the controllers via the viewmodel. That way a view-controller pair becomes a completely reusable component that can be shown in multiple ways from multiple locations as part of different flows, and even using different models.
I don't always bother with all of the above, but it goes to show just how many separate concerns can actually end up in a controller, or instead be extracted to its own object: Fetching the model (provider), formatting the data (presenters), mutating the model (service/dispatch objects), wrapping the model (viewmodel), local ui state (viewmodel) and navigation between views (flow coordinator). It really goes to show that MVC isn't so much an architecture as it's the foundation for one; You've separated your view and model - great, now what about the rest of the application?
While outlining the post, I was uncertain what to call the particular group of classes (or concern) now called the Controllers. If you look through the GitHub project, you'll find that at one point they were called Routers. I agree with you that this design is not a MVC by the traditional definition (i.e. Smalltalk's), however there are many variants out there, see for example Apple's https://developer.apple.com/library/archive/documentation/Ge..., that I didn't think it would hurt 'to add' another. As long as it's clearly defined what it is. But perhaps it is not clear.
In this design, the 'traditional controller' is already part of the front-end. One of the goals of this design is to keep the front-end only concerned with styling and the layout of the application, and not the logic or the flow. So from the front-end, the user-interactions will be forwarded down to the 'back-end' Controllers. Those controllers are responsible for the flow of the application. The back-end includes logic and models in the traditional sense, see for example the DocumentsModel (https://github.com/Fagrell/clean-editor/blob/master/lib/publ...), however there are also models that correspond to specific Components. Those "Model Components" contain logic and a state, e.g. the MenuModel referred to in the post.
Perhaps an even better separation would be to add another layer?
- UI front-end - View/Component layer -> includes the 'view and controller by the traditional definition'.
- Router/Controller layer -> includes Models for specific components and Routers (as Controllers are defined in the post). Defines the flow.
- Back-end layer -> includes models by the traditional definition.
Thoughts?
Personally, I hate the name controller because its really unclear to me what they actually do. What do they control? The actual update logic is part of the domain knowledge, so that should live in the model itself, as quietbritishjim said. Router is a better name imho (even if itself not a perfect name).
For example, the logic of inserting text when insertion mode is active, removing the selected text when the delete key is pressed etc is all part of the controller, not the view.
By splitting the view and the controller, a different view, for example a console one, or a remote one, can be done with the exact same editing capabilities, since they are to use the same controller.
> ...
> But operations on the data are just as much part of the model as the data itself.
So you want to move controller into the view or to the model? I can't tell. :)
I understand the term "model" to include non-graphical operations on model data. For example, SaveToFile() or InsertTextAtPosition() methods. These should definitely NOT be merged with the view!
--> I believe that the linked article called these non-graphical operations "controllers", but I disagree.
----------------
I understand the term "controller" to be graphical controls that allow manipulation of the data. For example, a the QMenuItem "save" along with its associated signal (which of course will just call a method on the model), or the OnKeyPress handler on an edit control.
--> I believe that the linked article just considers these to be part of the "view". I'm actually OK with this, because I the distinction between controller (in this sense) and view is not very useful.