0

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

Folling
  • 415
  • 3
  • 12
  • https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx learning this may help. – SedJ601 Jun 04 '19 at 20:54
  • See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – SedJ601 Jun 04 '19 at 21:03
  • This question isn't about "how does MVC work". I'm well aware of how to work with JavaFX, I'm also aware of how to use MVC. What MCVE do you expect from a question that is mostly about the abstract idea of datamodeling? There's nothing I could give you that you could test. I understand the necessity of MCVE's in questions directly regarding code - but this is about the idea of how to handle a specific situation. In this case: How do I handle the effective deletion of an Object in the rest of the application. – Folling Jun 04 '19 at 21:07
  • From your question, it seems you might not know as much as you claim. If you structured your project correctly, you would not have this problem. I would suggest you study James D example. It could also be that you are not explaining your problem good enough. Good luck with finding a solution. – SedJ601 Jun 05 '19 at 01:42
  • The structure of my project isn't really the problem, however I might be wrong about that. However MVC does not contain a solution for my datamodeling problem per se. My data is inherently interlinked, that's the requirement for the project, it's an odd usecase, I'm aware. But it is what it is. I appreciate your input either way and thank you. – Folling Jun 05 '19 at 10:54

0 Answers0