gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:30:59 24 4
gpt4 key购买 nike

我想知道使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com