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.
P.S. If you simply move the model into the controller, essentially it will be the same without extra files?”
Screenshot of the program structure:
Code:
public interface Contract {
public interface PrimaryView {
void showData(String data);
String getCurrentData();
void switchToSecondary() throws IOException;
void showList();
}
public interface SecondaryView {
void showData(String data);
void switchToPrimary() throws IOException;
}
}
In FXML JavaFX, everything operates through the controller, but here the controller duplicates all calls in the presenter.
public class PrimaryController implements Initializable, Contract.PrimaryView {
@FXML
private Label dataLabel;
@FXML
private Button primaryButton;
private PrimaryPresenter presenter;
@Override
public void initialize(URL url, ResourceBundle rb) {
presenter = new PrimaryPresenter(this);
presenter.init();
dataLabel.textProperty().bind(presenter.getUserModel().nameProperty());
}
@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 PrimaryPresenter {
private PrimaryView view;
private UserModel model;
public PrimaryPresenter(PrimaryView view) {
this.view = view;
this.model = new UserModel();
}
public void init() {
view.showData("PrimaryPresenter");
}
public UserModel getUserModel(){
return model;
}
public void showList(){
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
public void switchToSecondary() {
try {
App.setRoot("secondary");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Model
public class UserModel {
private StringProperty name;
private StringProperty email;
public UserModel() {
this.name = new SimpleStringProperty();
this.email = new SimpleStringProperty();
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
public StringProperty nameProperty() {
return name;
}
public String getEmail() {
return email.get();
}
public void setEmail(String email) {
this.email.set(email);
}
public StringProperty emailProperty() {
return email;
}
}