Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Immutable Model classes in ModelWrapper #491

Closed
manuel-mauky opened this issue Jun 9, 2017 · 0 comments
Closed

Support Immutable Model classes in ModelWrapper #491

manuel-mauky opened this issue Jun 9, 2017 · 0 comments

Comments

@manuel-mauky
Copy link
Collaborator

Sometimes developers want to use immutable model classes. In this situation the ModelWrapper doesn't work because there is no classical "setter".

The ModelWrapper could provide a way to define fields by using an "Immutable setter" which is a function on an object that takes an argument for a field and returns a new instance of the object with this field changed:

public class ImmutablePerson {
    private final String name;

    public ImmutablePerson(String name) {
        this.name = name;
    }

    public ImmutablePerson withName(String newName) {
        return new ImmutablePerson(newName);
    }

    public String getName() {
        return name;
    }
}


ImmutablePerson hugo = new ImmutablePerson("Hugo");
ImmutablePerson hans = hugo.withName("Hans");

The ModelWrapper should provide methods like this:

ModelWrapper<ImmutablePerson> modelWrapper = new ModelWrapper<>();
StringProperty name = modelWrapper.fieldImmutable(ImmutablePerson::getName, ImmutablePerson::withName);

This way the user can change the name in the UI without actually changing the immutable person model that is behind the UI in the viewModel. Instead in the ViewModel one can get a new immutable person instance out of the ModelWrapper that has the new values from the UI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant