【问题标题】:Does this void the MVC design pattern?这是否会使 MVC 设计模式无效?
【发布时间】:2019-10-24 22:18:11
【问题描述】:

我想知道使用控制器将本地学生(模型)拉到我的视图类中是否会使 MVC 设计模式无效。

F.Y.I

我从不将我的 Student 模型导入到视图类中。


控制器

public void saveStudent(int selectedRow, Student studentChanged){
    studentList.getStudentList().set(selectedRow, studentChanged);
}

查看

Student currentStudent;

。 . . .

public StudentDetailedUI(StudentCntrl studentCntrIn, int selectedRowIn) {
    studentCntrl = studentCntrIn;
    selectedRow = selectedRowIn;
    if (selectedRow >= 0) {
        currentStudent = studentCntrl.getStudent(selectedRow);
        initComponents();
        parseCurrentStudent();
    } else {
        initComponents();
        parseNewStudent();
    }
}

。 . . .

JButton saveButton = new JButton("Save");
    saveButton.addActionListener((ActionEvent e) -> {
        if (selectedRow != -1){
            currentStudent.setFirstName(firstNameDisplayValue.getText());
            currentStudent.setLastName(lastNameDisplayValue.getText());
            currentStudent.setUniversity(universityDisplayValue.getText());
            currentStudent.setGpa(Double.parseDouble(gpaDisplayValue.getText()));
            StudentDetailedUI.this.studentCntrl.saveStudent(selectedRow, currentStudent);
            StudentDetailedUI.this.studentCntrl.getStudentListUI();
        }
        else {
            StudentDetailedUI.this.studentCntrl.addStudent(firstNameDisplayValue.getText() +", " +lastNameDisplayValue.getText() +", " +universityDisplayValue.getText() +", " +gpaDisplayValue.getText());
            StudentDetailedUI.this.studentCntrl.getStudentListUI();
        }
    });

我的预期功能是使用列表详细信息 GUI 更新列表中的现有学生。

【问题讨论】:

  • TL;DR: 做或不做都没关系,“MVC”在不同的框架和不同的人中意味着不同的东西。否则您将如何将数据放入视图中?
  • 我可以将它转换为字符串,因为它是从控制器中拉出的。然后将编辑好的字符串发送给控制器,然后将字符串解析成一个学生模型。但是我觉得它有很多不必要的代码。
  • 那还是传一个模型,正好被序列化了。

标签: java model-view-controller


【解决方案1】:

只要有关更新的所有逻辑都保留在控制器中就可以了,最终您可以在视图中添加一些验证,但控制器仍然应该对联系持久层有最后决定权。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-30
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 2010-12-24
    • 1970-01-01
    相关资源
    最近更新 更多