Java – MVC: View-Controller Interaction

javamvc

I have a Java application that I am implementing following the MVC pattern.

I want to have a window (view) that contains a form which allows the user to add several text fields. There will also be a button that will allow the user to add more text-fields to the form.

I have several questions about this.

Will the code for adding new text-fields be considered part of the controller or the view?

  1. If controller, then this controller will not do anything to the model, it will just modify my view, so is this a violation of MVC?
  2. If view, then will it be a violation of MVC as the changes in view were not directed from the model?

Best Answer

A couple of things here:

  • There are several ways of implementing MVC, each technology and kind of app has it. See this great article to have a better understanding.

  • An action that involves only VC is not a violation, not every user action has to touch the three parts of MVC.

    1. The code that will add new text-fields will be controllers, which add a view (textfield) and should add a model representing what that textfield means in your app (if the view alone has any meaning then this VC-only relationship is ok).

    2. Changes in the view can come from two places: from the controller or from the model itself BUT by notifications (this means that the model must not know the views directly, but have a group of listeners that listens his changes).

I hope this answer plus the article clarifies a little bit more your knowledge of MVC and its implementation in your app.

Good luck!

Related Topic