samedi 30 mars 2019

Is it ok for an Observer to hold references to the Observable?

I'm working on a group project for School and we're making an application in JavaFX. We have some trouble understanding the Observer Pattern and wanted some confirmation whether or not a simple example is correct.

This is a minimal example of a TreeView in JavaFX that observes a list of objects.

The TreeView fxml

<fx:root type="javafx.scene.control.TreeView" xmlns="http://javafx.com/javafx/8.0.162-ea" xmlns:fx="http://javafx.com/fxml/1">/>

The TreeView controller class

public class RoundViewController extends TreeView<Round> {
    private ObservableList<Round> rounds; //reference to the model

    RoundViewController(ObservableList<Round> rounds) {
        super(new TreeItem<Round>());
        setShowRoot(false);

        this.rounds = rounds;

        rounds.addListener((ListChangeListener<Round>)(c -> {
            while (c.next()) {
                if (c.wasAdded()) {
                    addRounds(c.getAddedSubList());
                }
                if (c.wasRemoved()) {
                    removeRounds(c.getRemoved());
                }
            }
        }));
    }

    private void addRounds(List<? extends Round> rounds) {
        rounds.forEach(round -> {
            TreeItem<Round> treeItem = new TreeItem<>(round);
            getRoot().getChildren().add(treeItem);
        });
    }

    private void removeRounds(List<? extends Round> rounds) {
        List<TreeItem<Round>> treeItemsToRemove = getRoot().getChildren().stream()
                .filter(treeItem -> rounds.contains(treeItem.getValue()))
                .collect(Collectors.toList());
        getRoot().getChildren().removeAll(treeItemsToRemove);
    }

}

The Main class

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        ObservableList<Round> rounds = FXCollections.observableArrayList();
        RoundViewController roundView = new RoundViewController(rounds);

        primaryStage.setScene(new Scene(roundView));
        primaryStage.show();

        rounds.add(new Round("Music round"));
        rounds.add(new Round("Guess the facts"));
        rounds.add(new Round("Let's round it up"));
    }


    public static void main(String[] args) {
        launch(args);
    }

Aucun commentaire:

Enregistrer un commentaire