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

2

14

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 "Model" part of the MVC pattern; that is, almost all of my code is the OO-representation of abstractions in the sense of classes, and all of that code is logical code.

Since I do not want to be the only user of my program, I want to add the "View" part of MVC so that people can easily use and manipulate the "Model" part of my program. For this, I want to use JavaFX.

In my "Model" classes I obviously use various Lists, Maps, and other classes from the Java Collections API. In order to let the users of my program manipulate these underlying Lists and Maps I want to use the Observable(List/Map) interfaces in JavaFX.

A concrete example to bring clarity to the situation:

Let's say that I have a MachineMonitor class that every 3 minutes checks certain properties of a Machine, such as if the connection is still good, the speed that the gears are turning, etc. If certain inequalities are met (say that the speed of the gears has fallen to a rate of 1 turn/sec) the MachineMonitor fires a RestartMachineEvent.

Currently I use an ArrayList<MachineMonitor> to keep track of all of the individual MachineMonitor's. Now extending to the "View" part of MVC, I want the User to be able to manipulate a TableView that displays the list of MachineMonitors so that they can, for instance, create and remove new MachineMonitor's to monitor various Machines.

So that I can keep track of what the user of my program wants to do (say, create a MachineMonitor for Machine #5 that checks to see if the turn/sec of the gears falls below 0.5) I use an ObservableList<MachineMonitor> as the underlying List for the TableView.

The easiest way to link the "Model" and "View" of my program would simply be to change the "Model" class to have an ObservableList<MachineMonitor> and not an ArrayList<MachineMonitor> but (getting to the topic of the question) I feel that this is very messy because it mixes "Model" and "View" code.

A naïve approach would be to use an ObservableList<MachineMonitor> for the TableView and retain the use of my ArrayList<MachineMonitor>. However, changes made to the ObservableList<MachineMonitor> do not affect the underlying List as per the JavaFX specifications.

Given this, is the best way to solve this conundrum to make a ChangeListener for the ObservableList<MachineMonitor> that "propagates" the changes made to the ObservableList<MachineMonitor> to the underlying "Model" ArrayList<MachineMonitor>? Perhaps put this in a class called MachineMonitorController?

This ad-hoc solution seems very messy and non-ideal.

My question is: What is the best way to retain nearly complete separation between the "Model" and "View" in this scenario?

Yippie answered 14/5, 2014 at 0:47 Comment(2)
Although I respect and understand the MVC concept importance, I'd like to say that you must always evaluate if you really need it here. Usually, V and C come together because it's rare to find useful frameworks without these two somewhat coupled. Getting a real MVC independence is hard and demands a lot of indirection code. There's really a sweet spot between a real MVC and choosing some nice framework and stick to it in the long run, IMO.Markswoman
I think that the API of the JavaFX TableView is really annoying. I don't understand why there are no simple methods, such as table.getModel().set(row, col, data). And I agree and do "feel" that it is against MVC. I want the data to be updated when I tell, and not to let the GUI do it for me automatically. If I wanted it to be updated automatically, I would use Microsoft tools and not Java (and I really don't like to do it)... It feels like programming with WPFCate
L
11

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<MachineMonitor> myNonObservableList = ... ;

ObservableList<MachineMonitor> 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.

Lauren answered 14/5, 2014 at 1:1 Comment(4)
I agree with you to a certain degree because javaFX packages such as javafx.scene.control.* are the UI ones (real "View" here) and so ObservableList is something between "Model" and "Control" (more like Model, I guess). On the other side, if you think on JavaFX as a presentation library, I am not sure if the developer decides to switch to another "View" (let's say JSF for example), if this dependency from JavaFX libraries isn't somehow a MVC problem. On the other hand, it's very rare to find real useful "Views" without their own "Controllers".Markswoman
Updated to address this comment.Lauren
Second link is no longer available.Reinhardt
JavaFX is not anymore (since Java 11) a part of Java SE.Arundel
D
12

Briefly, I don't think use of ObservableList breaks the MVC contract.

The rest, you may read or not as you wish, as it is quite annoyingly long.

Architectural Pattern Background

Observables are useful in MVC style architectures because they provide a way of feeding data back and forth between the MVC components through loose couplings where the model and view classes don't need to refer directly to each other, but can instead work with some shared data model which communicates data flow. It's not a coincidence that the Observable pattern and the MVC style architecture concept both originated around the same time at Xerox PARC - the things are linked.

As noted in Martin Fowler's GUI architectures, there are numerous different approaches to building GUIs. MVC is just one of these, kind of the granddaddy of them all. It is nice to understand MVC well (it is often misunderstood) and MVC concepts are applicable in many places. For your application you should use the system which feels best for you rather than rigidly following a given pattern (unless you are using a particular framework which enforces a given pattern) and also be open to adopting different patterns within an application rather than trying to shoehorn everything into a single conceptual framework.

Java Beans are a fundamental part of almost all Java programs. Though traditionally often only used in client apps, the observer pattern, through PropertyChangeListeners, has been, for good reason, a part of the Java Bean specification since it was created. The observable and binding elements of JavaFX are a rework of that earlier work, learning from it to build something that is both more convenient to work with and easier to understand. Perhaps, if the JavaFX observable and binding elements had existed ten or twelve years ago as part of the JDK, such concepts would be more generally used in a wider variety of libraries and frameworks than a couple of pure GUI frameworks.

Advice

I suggest considering the MVVM model and other GUI architectures.

If you want a dead-easy framework which follows a model, view, presenter style, definitely give afterburner.fx a spin.

I think the correct choice of architecture depends on your application, your experience and the size and complexity of the problems you are trying to solve. For instance, if you have a distributed system, then you could follow REST principles rather than (or in addition to) MVC. Whichever you choose, the architecture should aid you in solving the problem at hand (and possibly future problems) and not the converse. Over-architecting a solution is a common trap and is very easy to do, so try to avoid it.

Caveat

One caveat to consider is that observables necessarily work via side-effects which can be difficult to reason about and can be antithetical to the concept of isolation. JavaFX features some good tools, such as ReadOnlyObjectWrapper and ReadOnlyListWrapper, to help limit the impact (damage control if you like) on observables so they don't run amok in your system. Use such tools (and immutable objects) with reckless abandon.

Learn from Examples

For a simple JavaFX application which is built using observables, refer to tic-tac-toe.

For a good way to structure a large and complex JavaFX application with FXML based components, refer to the source code for SceneBuilder and SceneBuilderKit. The source code is available in the JavaFX mercurial source tree, just check it out and start learning.

Read up on the JavaFX UI controls architecture. Examine the JavaFX controls source code (e.g. Button and ButtonSkin or ListView and ListViewSkin) to see how concepts such as MVC can be applied using JavaFX structures. Based on that learning, try creating some of your own custom controls using the architecture that the JavaFX controls framework provides. Often, when you are building your own application you don't need to create your own controls (at least ones which derive form JavaFX Control). The JavaFX Controls architecture is specially crafted to support building libraries of reusable controls, so it is not necessarily generally suitable for all purposes; instead it provides a concrete demonstration of one proven way to get certain things done. Adopting and adapting proven solutions goes a long way to ensuring you don't reinvent stuff needlessly and allows you to build on a solid base and learn from the trials of others.

Regarding your Concrete Example

I advise you to go with:

The easiest way to link the "Model" and "View" of my program would simply be to change the "Model" class to have an ObservableList and not an ArrayList

Maybe use a ReadOnlyListWrapper to expose the ObservableList from the MachineMonitor to the outside world, so that nothing can modify it unduly.

Setup some other structure which encapsulates the view (for example a ControlPanel and ControlPanelSkin) and provide it a reference to the read only observable list of MachineMonitors. The ControlPanelSkin can encapsulate a TableView, a graph or whatever visual knobs and widgets you want to use for the user to monitor the machines.

Using such a structure effectively isolates your view from the model. The model really doesn't know anything about the UI at all and ControlPanelSkin implementation could be changed out to a completely different visual representation or technology without changing the core MachineMonitor system at all.

The above just outlines a general approach, you'll need to tweak it for your specific example.

Dozer answered 14/5, 2014 at 4:54 Comment(0)
L
11

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<MachineMonitor> myNonObservableList = ... ;

ObservableList<MachineMonitor> 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.

Lauren answered 14/5, 2014 at 1:1 Comment(4)
I agree with you to a certain degree because javaFX packages such as javafx.scene.control.* are the UI ones (real "View" here) and so ObservableList is something between "Model" and "Control" (more like Model, I guess). On the other side, if you think on JavaFX as a presentation library, I am not sure if the developer decides to switch to another "View" (let's say JSF for example), if this dependency from JavaFX libraries isn't somehow a MVC problem. On the other hand, it's very rare to find real useful "Views" without their own "Controllers".Markswoman
Updated to address this comment.Lauren
Second link is no longer available.Reinhardt
JavaFX is not anymore (since Java 11) a part of Java SE.Arundel

© 2022 - 2024 — McMap. All rights reserved.