1

Using the MVC pattern, asume a simple situation where you want to send a number (for example the number of pages in a book) with a controller to a setter.

Now, the set method looks like this

public void setPages(int pgs)
{
pages = pgs;
}

and the controller, which retrieves the information from a textbox in view, recieves this as a String.

Where would we perform the conversion?

Would we have the controller do

int pgs = Integer.parseInt(String textFromView);
someClass.setPages(pgs);

Or would it be better to have a setter which accepts string input from the controller, and does the conversion itself?

If we do this conversion in the setter, would that not imply business logic is getting into our controller?

2

The conversion to int should take place in the Controller.

The fact that the user enters the number of pages into a control that was designed for general text input is a pure User Interface concern and that should not bleed through to the Model.

Similarly, if the value is received over a text-based communication channel (such as HTTP), the conversion should also take place in the Controller.

All the Model should care about is that it gets a number that it can use in subsequent calculations. The Model should not care about all the ways that the number could be communicated from the user to the Model. That is the responsibility of the View and Controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.