'Applying MVC With JavaFx

I'm new to the GUI world/OO design pattern and I want to use MVC pattern for my GUI application, I have read a little tutorial about MVC pattern, the Model will contain the data, the View will contain the visual element and the Controller will tie between the View and the Model.

I have a View that contains a ListView node, and the ListView will be filled with names, from a Person Class (Model). But I'm a little confused about one thing.

What I want to know is if loading the data from a file is the responsibility of the Controller or the Model?? And the ObservableList of the names: should it be stored in the Controller or the Model?



Solution 1:[1]

What i want to know is that if loading the data from a file is the responsibility of the Controller Or the model?

For me the model is only responsible of bringing the required data structures that represent the business logic of the application.

The action of loading that data from any source should be done by the Controller Layer. You could also use the repository pattern, which can help you in abstracting from the type of source when you are acessing the data from the view. With this implemented you should not care if the Repository implementation is loading the data from file, SQL, NoSQL, Web service ...

And the ObservableList of the names will be stored in the controller or the model?

For me the ObservableList is part of the View. It is the kind of data structure you can bind to JavaFX controls. So for example a ObservableList<String> could be populated with Strings from the model but the ObservableList reference should be an attribute of some View´s class. In JavaFX it's very pleasant to bind JavaFX controls with Observable Properties backed by domain objects from the model.

You could also have a look to viewmodel concept. For me a JavaFX bean backed by a POJO could be considered as a view-model, you could see it as a model object ready to be presented in the view. So for example if your view needs to show some total value calculated from 2 model attributes, this total value could be an attribute of the view-model. This attribute would not be persisted and it would be calculated any time you show the view.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abra