Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我是否正确实现了MVP被动视图?_Java_Mvp - Fatal编程技术网

Java 我是否正确实现了MVP被动视图?

Java 我是否正确实现了MVP被动视图?,java,mvp,Java,Mvp,我目前正在使用JavaFX开发一个桌面应用程序(请注意,我没有使用屏幕生成器,而是直接在编码中创建视图)。我想实现MVP(模型视图演示者)模式的被动视图变体 因为我找不到任何明确的例子。。我试着自己创建一个基本的设置 主要课程 public class Main extends Application { public static void main(String[] args) { launch(args); } @Override pub

我目前正在使用JavaFX开发一个桌面应用程序(请注意,我没有使用屏幕生成器,而是直接在编码中创建视图)。我想实现MVP(模型视图演示者)模式的被动视图变体

因为我找不到任何明确的例子。。我试着自己创建一个基本的设置

主要课程

public class Main extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {

        Model model = new Model();
        View view = new View(stage);
        Presenter presenter = new Presenter(model, view);
    }
}
型号

public class Model {

    private StringProperty labelText;

    public Model() {
        this.labelText = new SimpleStringProperty();
    }

    public String getLabelText() {
        return labelText.get();
    }

    public StringProperty labelTextProperty() {
        return labelText;
    }

    public void setLabelText(String labelText) {
        this.labelText.set(labelText);
    }
}
查看

public class View {

    private Button button;
    private Label label;

    public View(Stage stage) {

        label = new Label("This is a test");
        label.setLayoutX(50);
        label.setLayoutY(50);
        button = new Button("Click me");
        button.setLayoutX(200);
        button.setLayoutY(50);

        Pane pane = new Pane();
        pane.getChildren().addAll(label, button);

        Scene scene = new Scene(pane, 400, 200); //Standard size 1200, 800
        stage.setScene(scene);
        stage.show();
    }

    public Button getButton() {
        return button;
    }

    public void setButton(Button button) {
        this.button = button;
    }

    public Label getLabel() {
        return label;
    }

    public void setLabel(Label label) {
        this.label = label;
    }
}
演示者

public class Presenter implements EventHandler<ActionEvent> {

    private Model model;
    private View view;

    public Presenter(Model model, View view) {
        this.model = model;
        this.view = view;

        //Register action listener for button
        this.view.getButton().setOnAction(this);

        //Register change listeners of model
        this.model.labelTextProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                //Update view
            }
        });
    }

    @Override
    public void handle(ActionEvent event) {

        if (event.getSource() == this.view.getButton()) {
            //Update model
        }
    }
}
公共类演示者实现EventHandler{
私有模型;
私人视野;
公共演示者(模型、视图){
this.model=模型;
this.view=视图;
//为按钮注册操作侦听器
this.view.getButton().setOnAction(this);
//注册更改模型的侦听器
this.model.labelTextProperty().addListener(新的ChangeListener()){
@凌驾

公共无效改变(ObservalEvalue在我看来还行。不确定MVP中的被动词指的是什么。所以你问题的答案是,是的,你似乎做得对