JavaFX – the right way to use Properties with domain objects

domain-objectsjavafxproperties

JavaFX has provided a bunch of new Property objects, such as javafx.beans.property.DoubleProperty which allow you to define fields which can be automatically observed and synchronised.

In many JFX examples, the MVC model class has a number of these Property fields, which can then bind automatically to the view.

However, this seems to be encouraging us to put JFX properties into our Domain objects (if you assume that the Model class is going to be a domain object), which strikes me as a poor separation of concerns (i.e. putting GUI code in the Domain).

Has anyone seen this problem being solved in 'real life' and, if so, how was it done?

Best Answer

I have been playing around with JavaFX 2.0, which I assume your question is about. Not real production code, just a personal project, but I ran into the same problem that you mention above. The entire model tends to become dependent from the 2D framework, and I don't like it.

What I did that I split every single class in the model in two, the real model class, which has the capabilities to load its contents from database, knows how it alters its state etc etc... and the representation class that decides the appearance on screen. The latter would contain all the Property classes.

You'll find the same design in any MVC framework, like Swing. it's just that here there's no escape from doing it.

Related Topic