r/JavaFX • • 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.

16 Upvotes

38 comments sorted by

View all comments

Show parent comments

1

u/Capaman-x Mar 16 '23

Thank you for taking the time to give feedback. You did a surprisingly detailed deep dive on the code. That must have taken some time. I appreciate all the feedback. I love learning new ways to code. I have learned a ton here.

Your biggest issue was my use of a void builder method accepting the parent container as a parameter. This was simple to split all the Menus off into their own builders and then adding them to the MenuBar. I did note however that you use that technique to add listeners and binds.

private void styleAndSizeTile(StackPane stackPane)From HexMap example

The bad method name of MenuWidgets.Configure() was the original author's code. I tried to reorganize without rewriting as much as possible. That said menuItemOf()is nicer.

Making names of Builders more descriptive? sure. That said, it's in the same class, one only has to look at build() to get a handle.

LogHelper as you mentioned was a mess. There was no need for a bind there let alone a bidirectional bind. There was also no need to move it out of view as all it did was update the Model. For some reason I never think of using Enums. You were correct there, an Enum inside the Log interface was a better solution.

The part about using Runnable over Task also correct. It was the original authors code left untouched.

I agree with setToggleDark() being moved, if anything for easier change of a theme. I am pretty proud of how I fixed this one. I changed the button to a ToggleButton, created its own builder and bound it to a property in the model. I then put a ChangeListener on the property and used it to change the text on the button. It also used Consumer<Boolean> isDark to call the following method in the Controller:

private void setToggleDark(Boolean isDark) {
if (isDark) Application.setUserAgentStylesheet(new PrimerDark().getUserAgentStylesheet());
else Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
}

So I was able to move the offending code to the controller while maximizing view code in the view.

The only thing I didn't change was openFileDialog()in this case, all it does is send text to the model. Perhaps it should be called getFileNameAndTurnItToAStringAndPutItInTheModelUsingAnotherViewOnAnotherStage() Jokes aside, it's an oddball thing with this framework. Probably should go in the controller.

I think what you need is a giant FAQ which covers all the rules, with examples. Subjectivity leads to inconsistency. I like your framework. It turned my brain inside out for awhile, but became more clear the more I used it. I think the most difficult part is trying to figure out the rules....

Oh I also would like to say that I love your scene resize method in your MineSweeper example. What a work of art! On your blog you have an incorrect link. Building Multi-Screen Applications with MVCI sends you to the introduction.

1

u/hamsterrage1 Mar 16 '23

I think you are so close on the ToggleButton stuff. If you remember that your Listener is on a property in your Model, then you realize that you don't need to put it in your View and you don't need to pass a consumer to the ViewBuilder for it - it can all happen in the Controller!

Here's what I would do... First I changed the ToggleButton builder method to just this:

    private Node createToggleButton() {
        ToggleButton toggleDark = ButtonWidgets.nftToggleButtonOf("Dark", model.isDarkProperty());
        toggleDark.textProperty().bind(Bindings.createStringBinding(() -> toggleDark.selectedProperty().get() ? "Light" : "Dark", toggleDark.selectedProperty()));
        return toggleDark;
    }

More on the ButtonWidgets stuff later, but I moved the binding into it and made the method more generic.

Then I stripped out the extra Consumer and the associated code from the ViewBuilder.

Then I changed the constructor of the Controller to this:

    public Controller() {
        Model model = new Model();
        interactor = new Interactor(model);
        viewBuilder = new ViewBuilder(model, this::requestUserAttention);
        model.isDarkProperty().addListener((observable) -> {
            setToggleDark(model.isDarkProperty().get());
        });
    }

Is this "better"?? I think it all boils down to coupling. Now the View is all about the ToggleButton and nothing but the ToggleButton. It's bound to the Model, and it's Label changes dynamically - which is all View. What does the bound property in the Model do??? It doesn't care, the View's role is just to update it and reflect its value on the screen. Since you're using a Listener anyways (which is the normal way to use a ToggleButton), and not the onAction Event - there's no need to define the Listener in the View.

About ButtonWidgets... You made the methods in there very specific to the particular Buttons in your layout. So you've moved the configuration of the Buttons out of the layout code - which is good - but now you have to look somewhere else to see how they are configured - which is not so good.

If you make the ButtonWidget methods more generic and parameterize the stuff that makes them unique then a reader can see in the layout code what the Button does, but your layout code doesn't get clogged up with the details about how the Button got configured.

And, of course, you can re-use your ButtonWidget methods in some other project.

1

u/Capaman-x Mar 17 '23

Sometimes when your mind is focused on learning new things, you don't pay enough attention to the simple things. Never should have specialized static somthingOf methods. Good name for those I think would be SOM methods. Common patterns should have a name. Maybe they do? Anyway, I liked what you did with the ToggleButton in the Controller so much I implemented the same thing with the FileChooser and LogData. I created:

public record LogData(Log.LoggingType type, String message) { }

Fill it with info and put it in an ObjectProperty<LogData>in the model and use an invalidation listener to call the LogIt service to update the log. A big perk is the log can be easily updated anywhere in the Controller or the View.

I think there is no limit on what you can communicate between the View and Controller via the Model. Is there a reason not to wire everything that way?

1

u/hamsterrage1 Mar 17 '23

You can do that, but I'm not sure that you should. Here's why...

There are "actions" and "state changes". If you think about the Model as the "State" of your MVC, then I think it makes it a bit clearer. You have a Button, and generally it triggers an Event which in turn will usually lead to an Action. The results of the Action will probably be a change in the State/Model eventually, but it's still really an Action.

With a ToggleButton it's a bit different. The ToggleButton is a GUI device for representing and changing State. So linking it to the Model and the using a Listener to monitor the Model and trigger an Action when it changes is more in line with the way that ToggleButton works.

But when you have a MenuItem that (eventually) opens a FileDialog, that's actually starting off with an Action. Then you're using that Action to modify State, and then using a Listener to convert that State change back into an Action - then using that Action to update State.

It's hard to characterize that Property in your Model as an aspect of the State of the system. Sure, you could call it UserRequestToAddFileActive, or something like that but is that really expressing "State"???

One of the biggest hints that it's not, and that it's really acting as an "Action Messenger" is that you have to put it back once the file has been added. Which, of course, triggers the Listener so you have to filter out the "false" value from triggering the Action.

The biggest problem with it might be that it's "programming by side-effects", which is almost always an anti-pattern, or at least a code smell. You have a user that clicks on a MenuItem, and there should be as straight-line of code that you can follow that executes that action. But here you can't, it just updates the Model. It's not clear from the code in the View what the implications are (if any) of that change to the Model.

It's a side-effect, and it's programmed somewhere else.

Most programmers, when they look at a change of State they assume that is just what it is, a change of State. But here it's not, is it? It's a trigger for an Action that eventually will put the State back the way it started.

One last thing, though. I realize that this particular case is invoking a Dialog and they are modal so the rest of the GUI is locked until it closes, but in the more general sense of "Hey! Why don't we use this for everything?"...

Usually, when you trigger an action you want to disable the trigger while the action is running. Maybe you want to do some other things in your GUI when the action is completed besides just re-enabling the trigger. This starts to get a little difficult using the "Action Messenger" in the Model because now you have to have some signal in the Model that the Action is finished, and you'll need a Listener in the View to do that.

All of this is much easier when you treat the Action as an Action through the whole process, because then have an obvious hook to connect your wrap-up code when it completes.

I even have qualms about the ToggleButton stuff for the Dark/Light mode being caught by a Listener in the Controller. If it's possible that the mode might be changed in a variety of ways, and from a variety of sources, then it makes a bit more sense.

That ToggleButton also will generate a onAction Event when it's clicked. So it might be cleaner to have that Event trigger an Action by invoking a Consumer from the Controller, instead of using a Listener in the Controller.

In general I'm very, very leery of using Listeners in the Controller because it's almost always on indication of programming by side-effect. And that's almost always not good. But that's sometimes what you get, because the State that your listening to really is State, and it's fluid and updated from a variety of places and in a variety of ways and it makes sense to watch it and react to it when it changes.

So, there you go.

→ More replies (0)