首页 > 解决方案 > Does this void the MVC design pattern?

问题描述

I am wondering if having a local Student(Model) pulled through using a controller to my view class voids the MVC design pattern.

F.Y.I

I never import my Student model into the view class.


Controller

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

View

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

My intended functionality is to update an existing student in a list using a list-detail GUI.

标签: javamodel-view-controller

解决方案


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


推荐阅读