简体   繁体   中英

How to implement MVP in JavaFX

Before some time, i started looking for a pattern to decouple UI from a logic of my app. I decided to use MVP, but there is one significant problem which i cant solve. How can i inject a instance of presenter into view, if classes that implements Application, are launched from static method. There is also no choice to launch specific instance of class implementing Application, so parameters in constructor are useless. Also i do not use FXML, my view class is coded in java.

PS: Sorry for my english, as it's not my native language

You can pass a reference from say Main.java to a Presenter. In Main do this:

Presenter p = new Presenter(); // This is your Presenter class
p.setReference(this);          // Call a method in the presenter    

// and here is a method in Main.java just as an example
public StackPane getRootView(){
    return this.rootView;
}

Then in Presenter you have:

private Main main;

public void setReference (Main main) {
    this.main = main;
}

Your presenter can now call methods in Main eg

StackPane sp = main.getRootView();

You could also do this in the constructor of Presenter.

I have written a sample code to answer to this problematic.

https://github.com/oterrien/JavaFX_Presenter.git

The view interface provides the intention of the view.

For example:

  • getting and setting text
  • getting and setting result1
  • getting and setting result2
  • getting and setting event handler for the Add button

    The concret view is created from FXML file. Each field of the control is defined with @FXML. The action to be triggered when the button is clicked is also a method and is prefixed by @FXML.

    The concret view implements the interface by providing a mapping between @FXML fields and getting/setting methods. And the triggered method does just call the event handler.

    The concret view is also responsible of creating the presenter (which refers itself as view).

    That is the important point. The presenter acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view.

    For that purpose, the presenter should be able to call the view in order to set data and retrieve data once updated by user. That is why, the presenter contains a reference of the view. But it should also provide action to be done when view's event handlers are called.

    When a user clicks on button "Add", the method which is bound with FXML is called. This method call the EventHandler which has been set by the presenter. In other words, the presenter is responsible of registering its own method to the view's EventHandler.

    Finally, testing the presenter just consists in creating a mock of the view.

  • The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM