Does the use of ObservableList in JavaFX go against Model-View-Controller separation?

后端 未结 2 2022
梦谈多话
梦谈多话 2020-12-16 14:44

I am attempting a study of JavaFX because I want to use it as the GUI of my program. My question is essentially a conceptual one:

To date my program is mostly the \"

2条回答
  •  既然无缘
    2020-12-16 15:29

    I disagree that using an ObservableList in your "model" class violates MVC separation. An ObservableList is purely data representation; it is part of the model and not part of the view. I (and others) use JavaFX properties and collections in model representations in all tiers of my applications. Among other things in there, I point out how I use JavaFX properties that are (or can be, at least) bound to JSF. (I should mention that not everyone agrees with the approach of using FX properties on the server side; however I don't really see any way to make the argument that they are somehow part of the view.)

    Also, if you do

    List myNonObservableList = ... ;
    
    ObservableList myObservableList = FXCollections.observableList(myNonObservableList);
    myObservableList.add(new MachineMonitor());
    

    the observable list is backed by the non-observable list, so the change occurs in myNonObservableList too. So you can use this approach if you prefer.

提交回复
热议问题