I have a very tightly interlinked DataModel in my Application.
Now one thing I have for example is the Foo
class
public class Foo {
StringProperty nameProperty;
// ...
public void delete() {
// ...
}
}
I now have a class called FooReference
which is used to represent a reference to the class Foo which the user can set in the Application. The reason for having this class will become evident in a moment.
I am using JavaFX and hence my datamodel relies on properties which update themselves. Foo contains a nameProperty
which is used to display the Foo in the application. Whenever this nameProperty
is changed I want the application to be notified. This won't work if I just pass the Foo around as Foo itself is unable to produce ChangeEvents.
Now comes the problem: How can I notify the FooReference when the Foo is deleted, so that it clears its value?
A naive approach I had was to have a List<Runnables deletionHandlers
which are being called when Foo.delete()
is being called. This however seems to be subpar. I do not want to use an EventSystem either, if avoidable.
The last idea I had was to use a BooleanProperty deletedProperty
which is being toggled when Foo.delete
is being called to which everything else can listen.
I'm not really fond of any of those ideas. The listeners provoke the usage of many lambdas which can drain memory and performance, as well as some additional complexity with multithreaded environments. The EventSystem as I said I'd like to avoid, mostly for personal reasons. The Properties seem very consistent with the rest of the layout of the program and integrate nicely with JavaFX (could be used to show the user what was deleted, etc.), they however don't really feel "right" either to me. Is there a better solution, or perhaps even a common practice?
thanks in Advance
Folling