0
\$\begingroup\$

As far as I understand, when creating a project using the FXML JavaFX Maven Archetype, the project is initially designed with the MVC pattern. However, I wanted to convert it to the MVP architecture (don't ask why). Since I have almost no practical experience in this, only a bit of theory, I would like to know how wrong I am in this approach and what mistakes I am making. Below is a description of this attempt.

Methods in classes are irrelevant; it's all conditional. An answer is needed regarding organization and interaction.

If you simply move the model into the controller, essentially it will be the same without extra files?

Screenshot of the program structure:

enter image description here

Code:

public interface Contract {

    public interface ExampleView {

        void showData(String data);

        String getCurrentData();

        void switchToSecondary() throws IOException;  
        
        void showList();
        
    }
}

In FXML JavaFX, everything operates through the controller but here, the controller duplicates all calls in the presenter.

public class ExampleViewController implements Initializable, Contract.ExampleView {
    
    @FXML
    private ListView<String> listView;    
    
    @FXML
    private Label dataLabel;
    
    @FXML
    private Button primaryButton;
    
    private ExampleViewPresenter presenter;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        presenter = new ExampleViewPresenter(this);
        presenter.init();
        presenter.setListView(listView);
        presenter.setDataLabel(dataLabel);
    }

    @Override
    public void showData(String data) {

    }

    @Override
    public void switchToSecondary() {
        presenter.switchToSecondary();
    }
    
    @Override
    public String getCurrentData() {
        return dataLabel.getText();
    }

    @FXML
    @Override
    public void showList() { 
       presenter.showList();
    }
 
}

Presenter:

public class ExampleViewPresenter {
    
    private ExampleView view;
    private ExampleModelData model;
    private ListView<String> listView;      
    private ObservableList<String> items = FXCollections.observableArrayList();
    private Label dataLabel;
    
    public ExampleViewPresenter(ExampleView view) {
        this.view = view;
        this.model = new ExampleModelData();
        
        this.model.addDataListner(new ExampleModelDataListner(){
            @Override
            public void notifyListeners() {
                Platform.runLater(() ->  dataLabel.setText(model.getName()));
            }
        });
        
    }

    public void init() {

        view.showData("PrimaryPresenter");
               
        new Thread(new Runnable() {
            @Override
            public void run() {
                
                while(true){
                
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                         
                    model.setName(">" + Math.random() + "<");

                }  
            }
        }).start();
        
    }

    public ExampleModelData getUserModel(){
        return model;
    }
    
    public void setListView(ListView<String> listView) {
        this.listView = listView;
    }

    public void setDataLabel(Label dataLabel) {
        this.dataLabel = dataLabel;
    }    
    
    public ListView<String> getListView() {
        return listView;
    }
    
    public void showList(){  
        items.add("item1");
        items.add("item2");
        items.add("item3");  
        listView.setItems(items);
    }
    
    
    public void switchToSecondary() {
        throw new UnsupportedOperationException("Not supported Yet");
    }

}
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. \$\endgroup\$ Commented Jun 13 at 13:00
  • 1
    \$\begingroup\$ javafx support for binding is particularly suited to MVVM, for example, but nothing precludes adding a presenter, for example. \$\endgroup\$
    – trashgod
    Commented Jun 15 at 16:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.