java - 这是否会使 MVC 设计模式失效?

标签 java model-view-controller

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

仅供引用

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

<小时/>

Controller

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 更新列表中的现有学生。

最佳答案

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

关于java - 这是否会使 MVC 设计模式失效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58549811/

相关文章:

model-view-controller - MVC 的替代方案

asp.net-mvc - Resharper 5 : How do I set the default formatting style for inline code blocks?

php - 轻量级的PHP REST API

java - TestNG 电子邮件报告 - PKIX 路径构建异常

java - 如何关闭嵌入式 Undertow 应用程序?

java - 方法参数注入(inject)如何在 Java 中工作

java - 是否可以通过另一个Java程序控制Spotify客户端?

java - 使用 JodaTime 计算两个日期之间的月份,记住年份

model-view-controller - 是否可以将演示模型模式用于具有EMF域模型的基于GEF的RCP应用程序?

php - Laravel 中如何将一个值从一个 Controller 传递到另一个 Controller