简体   繁体   English

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

[英]Did I implement the MVP Passive View correctly?

I'm currently working on a desktop application using JavaFX (Please note that I'm not using the Screen Builder, I create my view directly in the coding). 我目前正在使用JavaFX处理桌面应用程序(请注意,我没有使用Screen Builder,而是直接在代码中创建视图)。 I would like to implement the Passive View variant of the MVP (Model View Presenter) pattern. 我想实现MVP(模型视图演示者)模式的“被动视图”变体。

As I could not find any clear examples.. I tried to create a basic setup by myself. 因为我找不到任何清晰的示例,所以我尝试自己创建一个基本设置。

Main Classs 主班

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);
    }
}

Model 模型

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);
    }
}

View 视图

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;
    }
}

Presenter 主持人

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
        }
    }
}

I thought the following things should be considered when implementing a passive view pattern: 我认为在实现被动视图模式时应考虑以下几点:

  • Model and View are completely separated 模型和视图完全分开
  • View creates and displays only the UI elements 视图创建并仅显示UI元素
  • Model contains the data of the application and the logic to change this data 模型包含应用程序的数据和更改此数据的逻辑
  • Presenter updates the model as well as the view 演示者更新模型和视图
  • Presenter registers action event listeners and updates the model if required (View -> Presenter -> Model) 演示者注册动作事件监听器,并在需要时更新模型(查看->演示者->模型)
  • Presenter listens to changes of the model and updates the view if required (Model -> Presenter -> View) 演示者侦听模型的更改并根据需要更新视图(模型->演示者->视图)

Using these information, I tried to build this basic structure for a model-view-presenter setup. 利用这些信息,我尝试为模型视图展示者设置建立此基本结构。 Did I implement the passive view correctly? 我是否正确实现了被动视图?

Looks OK to me. 在我看来还可以。 Not sure what the passive in MVP refers to. 不确定MVP中的被动指的是什么。 So the answer to your question is, yes, you seem to have done it right. 因此,您的问题的答案是,是的,您似乎做对了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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