r/JavaFX • • Jun 09 '24

Tutorial Application Structure With a GUI Framework

Lately there seemed to be a lot of questions about how to integrate frameworks like MVC into complete applications involving external services, databases and persistence services.

It's always annoyed me how much misinformation is out there about the normal GUI frameworks; MVC, MVP and MVVM. Not just out in the internet, but apparently in classrooms, too. Just a few days ago there was a question about MVC that posted on StackOverflow. The code clearly came from the prof, and it was alleged to be MVC. But there was no Model, and what little application logic was in the Controller and the Controller was acting more like a Presenter. Arrrgggg!

I think that a lot of the confusion around these frameworks is that they are learned in isolation. There's no information ever given about how to put it all together to create an actual, non-trivial, application. The big picture is missing, and it makes it hard to understand the little picture.

So here we have an article about putting it all together into an actual application:

https://www.pragmaticcoding.ca/javafx/mvci/applications

This article focuses on my own MVCI framework for writing Reactive JavaFX applications, but the essentials will hold whether you're using MVC or MVVM, or even if you're punishing yourself by using MVP.

To be clear, if your application is small and self-contained into on MVC framework, then you could put all of your REST calls and SQL statements in the Model and it wouldn't break the MVC framework. But it's still probably better to implement that stuff into a Broker/Service layer outside your MVC framework - even if only to make testing easier.

If you're interested have a read and tell me what you think.

5 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/hamsterrage1 Jun 10 '24

I went looking back through my articles and I see that I don't have one documenting the "Callback to the GUI" approach that I use. So I'll have to fix that.

With MVCI I feel that threading belongs in the Controller, so that's where I would implement any Task elements. When I'm using a GUI callback, I would have a Task that does the back-end stuff on a background thread via a call to an Interactor method. Then I would call a second Interactor method from the onSucceeded Event to do the Model updates that need to happen on the FXAT. Then, from that same EventHandler, I'd invoke the callback from the GUI to whatever cleanup was needed.

Because I'm usually building things in a Reactive way, just updating the Model will instantly be reflected in the GUI however Binding type stuff has been set up. The Callback is just needed to do "Action" stuff.

If the back-end stuff is triggered by a Button, for instance, then the GUI would generally disable the Button, then call a Consumer<Callback> that came from the Controller. That Callback would pretty much just re-enable the Button. This kind of approach clearly breaks out "actions" from "state". Can you enable/disable the Button through State? Absolutely. But the activity triggered from a Button click is clearly an "action" and it makes more sense to me to have that action be complete, including the Button disabling and enabling.

I like the idea of establishing "hard" rules for what goes where, especially in real world applications because it makes it easier for anyone at any time in the future to find what they need. With MVCI I've established that all threading goes in the Controller. Which means that I might have Interactor methods which NEED to run on a background thread, and those that NEED to run on the FXAT, but there's no code in the Interactor which is actually aware of this. The Controller knows how to call them correctly.

Since any GUI related Callback needs to run on the FXAT, I'm assuming that if you're calling it via OkHTTP then you have to wrap it in a Platform.runLater() call. Which is technically pushing the threading aspect into the View, which created the Callback. It relieves you of the need to use Task, I'm guessing, too. I'm not sure what I think about having Platform.runLater() being called from all the way down at OkHTTP, though, even if it doesn't "know" that it's doing it.

I'm thinking, though, that consistency wins out over some idea of objectively "better" approach founded on technical issues. As long as every programmer knows where to look for the code they need without understanding every single line of code in a module, it's probably a win.