samedi 6 août 2016

JavaFX, FXML and controllers: switch scenes, maintaining scene data

Hello programmers and architects!
I'm new here and... I think my english could be... terrible! :-D So... be careful with me... please.

I have an architectural question about JavaFX, FXML and the various methods to switch scenes, mantaining data among all scenes... I have used a personal solution and I need to know if it's an acceptabile solution or not (at the architectural level).

I'll try to explain my solution. I have used a sort of "inverse" of Observer Pattern: instead use many observers for each observable, I've used one observer and many observables. I have created a class named SceneObserver that implements java.util.Observer. Then I have created all my controllers which extend java.util.Observable. I have NOT used "fx:controller" inside the various FXML, because I need to add controller with code. I use SceneObserver in order to:

  • register SceneObserver as observer of all my controllers
  • load a specifc FXML file
  • create a new Scene with only an AnchorPanel as root element ad use the root.getChildren().add() method to append to the root element the loaded FXML
  • set a specific controller for the FXMLLoader (that I have used to load FXML)

When a certain type of situation happens in a scene and it's necessary to switch scene, the scene notifies to SceneObserver the change (Observer Pattern), SceneObserver destroys the old scene and loads the new one. In other words I'm using only one observer to observe all controllers (the inverse of the Observe Pattern).
All works, but I know that this is not enough!

So, the question is: speaking at the architectural level, is this one an acceptabile solution?

I know, this is not the only one and surely there are others better ways, but... what about this one?

Why have I used this solution?
Well, two reasons:

  1. load only necessary FXML (more rapid application)
  2. leave controllers decoupled

Thanks to all and... sorry for my english :-(

Aucun commentaire:

Enregistrer un commentaire