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. Commented Jul 29, 2022 at 7:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.