0

I'm struggling to fully understand MVC pattern, I found a lot of information on the web but they are really confusing because it seems there are various ways to do it.

What I understood is that the user interact with the view which will call a method of the controller which will modify the model. The model notifies the view that its state is changed, and the view in turn asks the model for the new state (this can be obtained with an observer pattern).

I need an example to understand all of that, because i'm very confused. In this example I have a view which will display all users (a photo, a name, a surname, an email) with a given name. The view is something like this:

enter image description here

Imagine the view will show a list of user retrieved from a database, how would you use MVC pattern to achieve this behaviour? More important which is the model and how can notify the view that the user searched for another name (and therefore the user list to show is changed)?

Thank you all.

1

2 Answers 2

2

First off, in a modern desktop application, there are better patterns than MVC for separating the user interface from the application logic. For example the MVVM pattern. The problem is that the MVC pattern expects a strict separation between the View (what is presented to the user) and the Controller (input from the user into the application), but most GUI frameworks don't support that strict separation.


In the MVC pattern, the Model part is everything of the application that does not directly deal with user-interaction. In the original formulation of the MVC pattern, which you seem to refer to in your question, the View part is the code that draws the dialog on the screen and the Controller part is the part that handles keypresses and mouse clicks.

The View has read-only access to the Model part, so it can retrieve the information it needs from the Model. In your example, the Controller would receive the name entered in the search field and the press on the "Search" button. The Controller would then tell the View to update the displayed list of users to include only the users with the provided name (or starting with that name if it is a prefix search).

To expand the example with notifications from the Model, suppose that somehow a user gets added to the database (or removed, or has their name changed, etc.). The Model would send a notification to the observers that something has changed in the list of users. As the View gets that notification, it retrieves the list of users again according to the current filter criteria and updates what gets shown.


In the MVVM (Model-View-ViewModel) pattern, which is related to the MVC pattern, the Model is again everything that doesn't deal with user interaction. Here, the View does all the user interaction, both display of content and receiving keypresses/mouse actions. This can be covered for a very large part by the Window and Widget objects (and subclasses) of a typical GUI toolkit. The ViewModel is in part a glue layer between the Model and View parts, transforming data as needed, and in part a model of view-specific data items, like the current selection in a list.

In the MVVM pattern, the interaction in your example would be that the View informs the ViewModel that the filter criteria for the user list have changed and what name to filter on now. The ViewModel then retrieves the list of users from the Model and applies the filter (or possibly, asks the Model to provide a filtered list). Then the ViewModel tells the View that the list of users to show has changed and the View retrieves the filtered list to show it.

If the Model changes externally, the ViewModel can be notified of that change via the Observer pattern and the ViewModel can retrieve the new (filtered) list and inform the View to update what gets show.

2
  • This is making me reasoning. Would you use MVVM pattern instead of MVC pattern as a solution to my problem? What about MVP pattern?
    – Diablo3000
    Commented Feb 25, 2020 at 16:02
  • @Diablo3000, I am less familiar with the MVP pattern, but I would indeed recommend either of those for modern desktop applications over the MVC pattern. Also consider which pattern matches best with the GUI toolkit you are using. Commented Feb 25, 2020 at 17:46
0

I'll try to make it as simplistic as possible.

MVC

The starting point, in this pattern, is the Controller. The Controller gets triggered by an HTTP request (as an example) and it deals with the data model. The only portion of the Controller that is aware of anything related to the views, is which View to show. The knowledge of what the View does is completely absent from the controller.

MVP

This pattern is used when there is an abstraction existing for the View, such as Controls in WinForms/WPF. The user interaction begins in the View, and the Presenter, which is a glue between the UI and the Data is aware of the specific functionality of the View. For example, the presenter has IView property, which is a contract that is implemented by the Control's functions. This way you can mock the IView interface and write unit tests for the presenter.

MVVM

This is used when there is a possibility of data binding between a View and a Datastore. For example, if you are using a grid that binds directly to a database source, you don't have to have an intermediate step to invoke actions based on whatever occurred in the view. Instead the control is bound directly to a data model. An example would be a data grid control that retrieves rows automatically as you scroll down.

You might also do the data binding manually by exposing data manipulating functionality in your ViewModel to the View.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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