Fio
Fio

Reputation: 3088

How to choose the number of controllers using MVC architecture

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.

Upvotes: 0

Views: 651

Answers (3)

VPK
VPK

Reputation: 3090

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.

Upvotes: 2

Raedwald
Raedwald

Reputation: 48692

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

Upvotes: 1

Rohan Kadu
Rohan Kadu

Reputation: 1399

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.

Upvotes: 1

Related Questions