0

I want to develop a Java based application and I am now designing the software architecture with MVC(Model-View-Controller). My question is on ideal way to determine the number of Controllers to control more than one model which are somehow related to each other? To be more specific, I have 1 View and 3 Models. Models are not independent i.e Board Model is formed of Unit Models and Unit Models have Trace Models of their own. The relation between models are dynamic. Is it ideal to use 1 Controller for them all or should I continue with 3 Controllers for each model and another different Model to control these 3 Controllers? Any help will be appreciated.Thanks.

Fio
  • 3,088
  • 2
  • 13
  • 23
  • I think one controller per view is good as long as you are not defining way too many functionalities per model. – VPK Dec 21 '17 at 10:28

3 Answers3

2

In the end, it all boils down to the existing system or personal choice. But if you are starting to build an application, you should consider using one model per controller per view. This should shape your application towards loose coupling.

Like so, suppose if you have a view which gathers information about project. So, the project model can have multiple processes which can be a separate model. Likewise, project can have team-members which maybe list of member model.

This way you can use multiple models with has-a relationship.

Finally, as I said, it always becomes the personal choice.

VPK
  • 3,010
  • 1
  • 28
  • 35
  • Your has-a relationship seems reasanable but I have one question in mind: if the models have a 'has-a' relationship, where will the dependencies be managed? One will add 'processes' to 'project' in controller rigth? Then all models have to be managed in controller which means one view-one-controller-many models. Am I correct? – Fio Dec 25 '17 at 11:52
  • Yes, in the controller or in service. I think model wise service is also a good design. So, the `processService' will return the processes according to the `projectId`. Finally, adding these `processModel` list to the `projectModel`. So basically, a project controller will have dependancy on `processService`. Generally, in frameworks like `Spring` these dependancies are handled by `SpringContainers`. We just have to specify how they should be added. – VPK Dec 26 '17 at 09:48
1

In Model-View-Controller or MVC your one view will be connected to one specific model whose business logic will be controlled by one controller.

As per your design even though you have three models there must be only one model which will be directly interacting with your view . So you should have only one controller who will be interacting with that model.

Other two models can get there business logic implemented from same controller via your main model or have separate classes to complete it's BL or functionality.

At last it will dependent on project specific requirements and you can slightly modify every design pattern as per your project architecture.

Rohan Kadu
  • 1,311
  • 2
  • 12
  • 22
  • Thanks for the answer. I will try to implement what I understand from your comment, correct me if I am wrong? In controller: Board() board = new Board(..); board.addUnit(new Unit(...)); board.addUnit(new Unit(..)); board.addTrace(board.getUnit(..).addNewTrace(..)); – Fio Dec 25 '17 at 12:00
  • It should be your approach only your UI don't have anything to change on Unit and Terrace models. If there are some UI elements which changes data of anyone of this model. Then you should have a different view and a controller for that model – Rohan Kadu Dec 25 '17 at 17:18
1

I have one controller per type of resource. In most REST designs, that corresponds to one controller per URI pattern.

Raedwald
  • 46,613
  • 43
  • 151
  • 237