samedi 29 août 2020

How to apply MVC and Decorator Pattern correctly to JavaFX?

I'm working on a project in JavaFX where I have defined a sort of "window-container", with custom controls to close the application or minimize it to an icon. Inside the AnchorPane that defines the scene, I have put a BorderPane that replaces its center with other Nodes when the application needs to go from a view to another.

Since I'm applying (or trying to, at least) MVC-pull-model, for every scene that my application needs, I have a view composed of an FXML file + the class that extends the JavaFX.Application class, a controller split in Graphic-Controller (to manage graphical elements with @FXML annotations) and Logic-Controller (to manage business logic and request to the model through beans).

I think that the MVC pattern is applied correctly, but I really accept suggestions and corrections.

However, until now I used to do this thing inside the Window Container Controller to replace the center node of my window-container's BorderPane.

public class WindowContainerController {
     private BorderPane container;

     public void replace(View node){ 
          this.container.setCenter(node.getRoot);
     }
     
}

Where View is a class that load FXML files through the help of an enum (ViewType) that contains the FXML paths. I posted the code below.

Since this thing requires that every Logic or Graphic controller has a reference to the WindowContainerController in order to obtain the container attribute, I don't know if this violates MVC, due to the fact that I have relationships between controllers (I don't know if controllers can talk to each other in MVC)

In order to fix this thing, I thought to the Decorator Pattern, to "decorate" the window-container borderpane with other nodes, but I don't understand how to apply the pattern correctly with the JavaFX framework since it requires the start() method to start render the application.

Is it correct to apply Decorator, or I should use other patterns? If I'm right, how can apply Decorator with JavaFX?

public class View {
    private Parent root;
    protected View(ViewType view) {
        try {
            FXMLLoader loader = new FXMLLoader(ViewType.getUrl(view));
            setRoot(loader.load());
        } catch (IOException e) {
            AlertFactory.getInstance().createAlert(e);
        }
    }

    public Parent getRoot() {
        return root;
    }

    public void setRoot(Parent root) {
        this.root = root;
    }

}

Aucun commentaire:

Enregistrer un commentaire