0

I'm trying to understand and use JAVAFX for a school project.

Almost every Tutorial i've read through, said, they we're using MVVM (Model-View-ViewModel). But they are just using a View and a ViewModel (see, no Model here).

I want to separate the Model from the ViewModel (thus from the View). I don't want to have references to a gui package in my model class. So no JavaFx Properties here.

So, i tried it: Let's say, I have a class named Person.

public class Person {
        private String name;
        private String street;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getStreet() {
            return street;
        }

        public void setStreet(String street) {
            this.street = street;
        }
    }

And a ViewModel named PersonViewModel

public class PersonViewModel  {

    private Person underlyingObject = new Person();
    private SimpleStringProperty name;
    private SimpleStringProperty street;

    public String getName() {
        return underlyingObject.getName();
    }

    public SimpleStringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.underlyingObject.setName(name);
        this.name.set(name);
    }

    public String getStreet() {
        return underlyingObject.getStreet();
    }

    public SimpleStringProperty streetProperty() {
        return street;
    }

    public void setStreet(String street) {
        this.underlyingObject.setStreet(street);
        this.street.set(street);
    }
}

That way means so much code and so many possible ways to forget something (when you add or delete a member of the model). And the BiDirectional Bindings are a big pain too.

Does anyone know a better way?

I want my Model classes to be "gui-free" because they have to move into a server project someday. If there is no way to separate the model from the viewmodel, that would be okay too, but then we should stop calling it MVVM in Java.

Benjamin Schäublin
  • 1,526
  • 1
  • 16
  • 26
  • 2
    It's not clear to me that using JavaFX Properties violates a class being "GUI-free". There is nothing inherently GUI-related about the JavaFX properties classes, other than the fact they are defined in a package that begins `javax.`. The name of the package does not make a class a UI class. However, consider using bound Javabean properties in your model, and then using JavaBeanPropertyAdapter classes, see e.g. https://stackoverflow.com/questions/23522130/javabean-wrapping-with-javafx-properties – James_D Jun 21 '17 at 20:03
  • @James_D, i think it is gui related, because if I would decide to go back to swing (or another Framework) someday, i expect that my model (gui-unrelated-classes) would work as they did before, because they are only data-containers. But thank you for that interesting link, it'll definity help me to achieve what I'm looking for! – Benjamin Schäublin Jun 21 '17 at 20:11
  • 1
    There is absolutely nothing stopping you using JavaFX properties in a Swing application. The properties and bindings API could quite easily have been factored out into a separate library - they don't rely on the JavaFX toolkit at all. (The same is not true, for example, of the JavaFX concurrency API.) – James_D Jun 21 '17 at 20:19

0 Answers0