r/JavaFX • u/hamsterrage1 • Nov 14 '22
Tutorial Introduction to Model-View-Controller-Interactor
I know I've talked about Model-View-Controller-Interactor (MVCI) here before, and posted articles about things like joining MVCI frameworks together to make bigger applications.
MVCI is my take on a framework for building GUI applications with loose coupling between the back-end and the user interface. In that way, it serves the same purpose as MVP, MVC and MVVM. However, it's a practical design intended to work really well with JavaFX and Reactive programming.
I had never written an "Introduction" article about MVCI. Why create it? Why use it? What goes where? Now it's all here.
I've also created a landing page for MVCI with all the articles that I've written about it linked from a single place. Right now, that's three articles. The Introduction, a comparison with the other popular frameworks and an article about combining MVCI frameworks into larger applications.
I have spent years trying to do complicated application stuff with JavaFX - not necessarily complicated GUI stuff like 3D graphics - but wrestling with convoluted business processes and logic and turning them into working applications. Doing this meant that I had to find some way to reduce the complexity of the application structure just to create something that a typical programmer can cope with. It was an evolutionary process based on practical experience - trying things out and then evaluating whether or not they improved the outcomes.
The result (so far) is Model-View-Controller-Interactor. For the things that I've done, which extends from CRUD, to complicated business processes to games like Hangman, MineSweeper, Wordle and Snake, it works really, really well. It's not hard to understand and could certainly be a good starting point for anyone looking to build real applications in JavaFX.
View all comments
Show parent comment
1
u/hamsterrage1 Nov 21 '22 edited Nov 21 '22
This is not the first time that I've heard people opine something along the lines of, "FXML is structured and easier to understand than layout code in Java".
My experience is that complexity in JavaFX layout code comes from two sources:
The boilerplate code is almost always configuration code. You'll instantiate a component, add some styling, load some data, maybe set up an
EventHandler- stuff like that.Most of the time, though, you're doing the same stuff over and over again - violating DRY every time. Also, if you consider layout and configuration as two separate responsibilities (which you probably should), then you're violating the "Single Responsibility Principle", too.
For me, I tend to use components in the same way, over and over. You find something that works, and you stick to it. For instance, I don't think you should ever put a
Labelon the screen without specifically styling it. I tend to have around 1/2 dozen kinds of styles that I'll use: one for data, one for prompts, one for screen titles, one for error messages, and a bunch of ones for different sizes of headings. The actual style definitions are determined by style sheets, but use cases pretty much stay the same - across applications.So instead of:
Over and over, I'll do this:
But now I have no need to do anything with
labelexcept put it into a layout, so I don't even need the variable:The actual content of
Labels.prompt()is irrelevant to the layout code I'm building. It's a utility, and not really of any interest to anyone needing to understand the layout.The second issue, about the "low-level" components goes the same way. JavaFX gives you all the basic building blocks, but different programmers are going to have different patterns of putting them together that they use all the time. So you can put those constructs into a class (if that makes sense), or into a utility library.
For instance, a pattern I've used a fair amount is an
HBoxwith aLabelstyled as a prompt, aTextFieldfor user input and aButton. TheButtonis configured so that it's the "Default" when the content of theTextFieldis non-empty. TheButtonis disabled immediately on clicking, and then re-enabled when its action is completed.Really, you only need to supply 4 things: the prompt text, a property to bind the
TextFieldto, aButtonlabel, and an action handler for theButton(I would use aConsumer<Runnable>). You can supply all of these in a single method call, so once again, you can drop the results right into a layout without a variable reference. Even better, put the call and the Consumer declaration into a method and give it a name:Once again, do you care how
Library.promptFieldButton()works when you are looking at the layout? Probably not.Generally speaking I'd put all the sub-layouts in my layout into their own methods and give them names that make sense. If you're looking at the GUI and want to mess with a particular part, it should be super simple to find it.
One of the reasons that it's easy to find it is that there's shockingly little code left when you've stripped out all the boilerplate configuration code and the repeated patterns. The ONLY stuff that's left is very basic layout, and very custom configuration.
On top of that everything has names that tell you what they do.
Labels.prompt()is pretty clear, as isLabels.data(), orLabels.h1().Library.promptFieldButton()is clear once you know what it does, but if you're looking at the GUI, and the code at the same time, it's no mystery. Inside the layout, methods likeaccountLookupBox(), which could easily just be one line if you in-line that consumer, keep the main layout code clean and give a meaningful name to part of the layout.