MVC Design – One vs Multiple Models for Multiple Views and Controllers

cdesign-patternsmvcqt

I have an issue with shared code in a MVC application. Application controllers/views ABC are different classes but use a common framework. It is a single class reused multiple times.

Let me introduce exhibit A, which shows a mock-up of the application's A's main window.

Mock-up

There are two main points of interest. To the left there is a table view, which shows a number of available rubber ducks. Each rubber duck has a number of attributes, which are not editable and mainly used to sort the table. To the right is a screen where you can, in this case, edit the appearance of the selected rubber duck. The table is used to select which entry should be shown here.

The real table view is a lot more complicated than just a standard QTableView, It has an own controller class, a view class, and right now an own model class.

Also the application has a controller, view and model. The table view model is initialized from the main application model when the application inits. Removal/Insertion of items is not a use case.

Let me introduce exhibit B, which is a new application sharing the table view.

enter image description here

Application B contains the same listview, but here the right view is used to compose an image of a rubber duck race. That means you can add one or more
rubber ducks from the table. Also, after application A, there have been a number of calculations resulting in more columns in the table (such as a cuteness value which can be used to sort the table).

Now since application B has entered the scene, the model needs to be adjusted to contain more values. That means we can:

  1. share the code with application A, whose model will then be cluttered with useless (uninitialized) entries which are only used in other Applications.

  2. clone the code with different models, which leaves us with seperate classes and code redundancy.

  3. modify the table view to use the application's model at the cost of needing to check in the table's view which roles exist in the current model and should be displayed.

Also, there are plans for Application C which builds pairs of rubber ducks, does matchmaking (assigns a similarity score of both ducks) which should be shown in the table instead of "singular" rubber ducks. This application's model will vary greatly from the previous ones. This makes option 1 (and probably 3) rather difficult to implement.

So I prefer option 3 while my colleague disagrees, preferring separated models.

How can I accomplish what I am wanting to do?


Image attribution disclaimer:

Cyber duck image: commons.wikimedia.org/wiki/File:Cyberduck_icon.png

Duck race image: commons.wikimedia.org/wiki/File:Leichlingen_-_Entenrennen_2010_-_Rennen_13_ies.jpg

Best Answer

How about secret option 4?

Extract the common code into helpers / base classes, and extend or include as necessary?

This removes the duplication, and avoids putting anything more than what is actually used in each model.

You end up with one model per controller, and some helpers and base classes that (provided you organized and named them carefully) will be obvious in their function.