java - 单个和多个对象的 MVC 模式的实现
问题描述
我已经使用 MVC 模式(教程:链接)实现了学生管理。我决定将学生分为“SingleStudentModel”和“MultipleStudentModel”,但我不确定这对我来说是否有意义。总的来说,我对我的解决方案不满意。是否可以使用一个控制器处理单个学生和多个学生?如果模型导入到视图类中可以吗(参见StudentView.java)?
我该如何改进这个项目?
提前致谢。
Student.java(模型,数据类(?))
public class Student {
private String name;
private int nr;
public Student(String _name, int _nr){
this.name = _name;
this.nr = _nr;
}
// get set
}
SingleStudentModel.java(模型)
public class SingleStudentModel {
private Student student;
// get set
}
StudentController.java (控制器 -> SingleStudentModel)
public class StudentController {
private SingleStudentModel model;
private StudentView view;
public StudentController(SingleStudentModel _model, StudentView _view){
this.model = _model;
this.view = _view;
}
// set get
public void updateView(){
view.printStudentDetails(model.getStudent().getName(), model.getStudent().getNr());
}
}
MultipleStudentModel.java(模型)
public class MultipleStudentModel {
private Collection<Student> students = new ArrayList<Student>();
public Collection<Student> getStudents() {
return students;
}
public void setStudents(Student student){
this.students.add(student);
}
}
StudentsController.java (控制器 -> StudentsModel)
public class StudentsController {
private MultipleStudentModel model;
private StudentsView view;
public StudentsController(MultipleStudentModel _model, StudentsView _view){
this.model = _model;
this.view = _view;
}
public void updateView(){
view.printStudentList(model.getStudents());
}
}
StudentView.java
public class StudentView {
public void printStudentDetails(String _name, int _nr){
System.out.println("Student: ");
System.out.println("name: " + _name);
System.out.println("nr: " + _nr);
}
}
学生视图.java
import com.mvc.model.Student;
import java.util.Collection;
public class StudentsView {
public void printStudentList(Collection<Student> students){
System.out.println("\nStudent list");
for(Student item : students){
System.out.println("name: " + item.getName());
System.out.println("nr: " + item.getNr());
}
}
}
主.java
public class Main {
public static void main(String [] args){
//Single student
SingleStudentModel model = new SingleStudentModel();
StudentView view = new StudentView();
StudentController controller = new StudentController(model, view);
model.setStudent(new Student("John", 1));
controller.updateView();
//Multiple student
MultipleStudentModel model2 = new MultipleStudentModel();
StudentsView view2 = new StudentsView();
StudentsController controller2 = new StudentsController(model2, view2);
model2.setStudents(new Student("Zelda", 2));
model2.setStudents(new Student("Link", 3));
controller2.updateView();
}
}
解决方案
您可以创建model
两个类的接口MultipleStudentModel
并将SingleStudentModel
实现
所以你不需要定义两个基本共享相同逻辑的控制器,这也可以让你在运行时更改控制器行为setModel
这还将创建一个合同,将您的模型绑定到特定方法,如 get 等(插入/更新/删除)
public class StudentController {
private Model model;
private StudentView view;
public StudentController(Model _model, StudentView _view){
this.model = _model;
this.view = _view;
}
public setModel(Model model){
this.model = model;
}
}
你也可以在View
课堂上做到这一点
另外,基本上一个学生只是一个学生列表,只包含一个元素,你可以删除冗余
推荐阅读
- neo4j - 匹配具有相同属性的节点之间的关系
- visual-studio-code - 在终端上打印不着色字符串
- java - java中对表的基本迭代
- javascript - 提交表单后反应输入状态未更新
- javascript - 如何在方法getActiveCell()上获取偏移一行的坐标
- php - 如何在 MySQL 中存储 AES 密钥
- tomcat - 无法在 IntelliJ IDEA 中运行 Tomcat
- javascript - 自定义钩子中的 useReducer 超过了最大更新深度,但 useState 没有
- javascript - Ant 设计表单“警告:`callback` 已弃用。请改为返回一个 Promise。” 自定义规则错误
- arrays - 合并两个带有子元素的数组