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.