2

I have an MVC application in Java with GUI components in a View class. The GUI has to support two languages, so texts on buttons etc. depend on which language was set at start up. Strings are in .properties files and I have a MyStrings.get(Key) to get the string corresponding to Key.

Should I call MyStrings.get(Key) in my View class constructor or should my Controller class pass the result of MyStrings.get(Key) to the View constructor?

Option1 - The View takes care of strings in its constructor:

View() {
    button.setText(MyStrings.get("BUTTON_TEXT"));
}

Option2 - The View gets the strings from the controler:

View(String buttonText) {
    button.setText(buttonText);
}

1 Answer 1

5

In a traditional MVC architecture, the view should interact with the model to get domain model data.

But fixed strings (i.e. not domain data) still belong to the user interface itself. The view should take care of them by itself and should get the strings directly. As in any localized MVC app.

If you would chose to get the controller to look-up for the strings, the controller would have to know lots of details about the views. Moreover, many ui changes to a view (e.g. add a new button) would then require to change the controller as well. This is not in the spirit of MVC which aims to encapsulate different concerns so to keep most changes local to a component (e.g. change in a view should not impact the model or the controller)

1
  • Beware of "fixed strings" in cases where post-deploy configurability is desired, i.e. the ability to alter the strings without involving a developer and/or redeploy.
    – Flater
    Jul 29 at 7:15

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.