jpaw
jpaw

Reputation: 51

Java MVC - Should view or controller prompt for input?

My program uses MVC. When the user hits "New File", I need to check if the previous open file is edited (and prompt the user to save if so).

From what I've understood, the controller should solely perform all validation and logic. Is it then okay for my controller to prompt the user for file name, save previous file etc using JOptionPane? Or should all input be taken in the GUI?

Thanks!

Upvotes: 1

Views: 2495

Answers (3)

shahensha
shahensha

Reputation: 2131

In the MVC pattern, "The controller receives user input and initiates a response by making calls on model objects".

Considering the context of your problem, especially the tasks which you mentioned:

  1. Prompt the user for file name
  2. Check whether file has been modified
  3. Save the changes, etc

To help you better understand, I'll tell you what task should be performed by which component.

Your model should have logic for performing 2 and 3. And your controller should invoke (call methods on) your model and depending on the returned values invoke yet other functionality on the view, like a using JoptionPane and other such things for prompting the user for file name etc.

So all in all, your controller should only act as a moderator and do nothing on it's own. All the tasks you mentioned will be performed either by Model or the View. It's the job of your controller to bring them about.

Good luck.

Upvotes: 2

Alberto Gutierrez
Alberto Gutierrez

Reputation: 1588

Well is very difficult to come up with an exact answer for design questions, but in this case, I would be very inclined to recommend you to implement this functionality in your view, the reason is that is pure UI what you are doing here, is just a set of operations to obtain an input that then you will pass to your controller.

Let's put it this way, your controller shouldn't care or know or intervene in how information is retrieved in your client, it should only handle specific requests, the controller should be UI agnostic so its interface shouldn't change if you get the file name through the UI or command line, or configuration..

Upvotes: 1

Java
Java

Reputation: 2489

Role of controller in MVC architecture

Controller receives a request.

Controller decides the requested activities based on request parameters.

Controller delegates tasks to be performed based on the request parameters.

Controller delegates the next view to be shown.

Go through this link

will give idea to your problem.

Upvotes: 1

Related Questions