我已经实施了一个MVC模式的学生管理(教程:Link)。我已决定将学生分为“SingleStudentModel”和“MultipleStudentModel”,但我不确定这在我的案例中是否有意义。总的来说,我对我的解决方案不满意。是否可以使用一个控制器处理单个学生和多个学生? 如果将模型导入视图类(参见StudentsView.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(Model)
public class SingleStudentModel {
private Student student;
// get set
}
StudentController.java(Controller - > 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(Model)
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(Controller - &gt; 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);
}
}
StudentsView.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());
}
}
}
Main.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();
}
}
答案 0 :(得分:2)
您可以创建model
的界面,以便MultipleStudentModel
和SingleStudentModel
这两个类实现
因此您不需要定义两个基本上共享相同逻辑的控制器,这也可以让您在运行时使用setModel
更改控制器行为
这也将创建一个契约,将您的模型绑定到特定方法,如get和such(insert / update / delete)
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
类
另外,基本上一个学生只是一个学生名单 一个元素,您可以删除该冗余
答案 1 :(得分:1)
有一些建议:
您可能听说过Interface-based programming,如果您有多个同类对象的实现,这是一种更好的方法,例如本例中的model
和view
。所以更好的方法是将这两个抽象为接口而不是类:
StudentView.java
import java.util.List;
public interface StudentView {
void printStudentList(List<Student> students);
}
StudentModel.java
import java.util.List;
public interface StudentModel {
List<Student> getStudents();
}
随时随地实现它们。
StudentController.java
import java.util.HashMap;
import java.util.Map;
public class StudentController {
// private StudentModel model;
// private StudentView view;
private Map<StudentModel, StudentView> modelAndViews; // store model-view couple
public StudentController() {
this.modelAndViews = new HashMap<>();
}
public void updateView(StudentModel model) {
StudentView v = this.modelAndViews.get(model);
if (v == null) return;
v.printStudentList(model.getStudents());
}
public void addModelAndView(StudentModel model, StudentView view) {
// validate for null pointer
// if (...)
this.modelAndViews.put(model, view); // add new model-view couple
}
// other methods
}
Student.java
public class Student {
private String name;
private int rollNo;
public Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
@Override
public String toString() {
return "Student (name=" + name + ", rollNo=" + rollNo + ")"; // for testing
}
}
以下是model
和view
的实现:
SingleStudentModel.java
import java.util.List;
public class SingleStudentModel implements StudentModel {
private List<Student> students; // store one student;
public SingleStudentModel(List<Student> students) {
this.students = students;
}
@Override
public List<Student> getStudents() {
return this.students;
}
}
MultipleStudentModel.java
import java.util.List;
public class MultipleStudentModel implements StudentModel {
private List<Student> students;
public MultipleStudentModel(List<Student> students) {
this.students = students;
}
@Override
public List<Student> getStudents() {
return this.students;
}
}
SingleStudentView.java
import java.util.List;
public class SingleStudentView implements StudentView {
@Override
public void printStudentList(List<Student> students) {
Student s = students.get(0);
System.out.println(s);
}
}
MultipleStudentView.java
import java.util.List;
public class MultipleStudentView implements StudentView {
@Override
public void printStudentList(List<Student> students) {
for (Student s : students) {
System.out.println(s);
}
}
}
(这两个视图可以组合成一个,而两个更容易理解:))
最后,它是主要的课程:
Main.java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Student> student = new ArrayList<>();
List<Student> students = new ArrayList<>();
// add test cases
student.add(new Student("s1", 1));
for (int i = 0; i < 5; i++) {
students.add(new Student("s" + i, i));
}
StudentModel singleStudentModel = new SingleStudentModel(student);
StudentModel multipleStudentModel = new MultipleStudentModel(students);
StudentView singleStudentView = new SingleStudentView();
StudentView multipleStudentView = new MultipleStudentView();
StudentController controller = new StudentController();
controller.addModelAndView(singleStudentModel, singleStudentView);
controller.addModelAndView(multipleStudentModel, multipleStudentView);
System.out.println("---------- update single student's view ---------------");
controller.updateView(singleStudentModel);
System.out.println("---------- update multiple students' views ---------------");
controller.updateView(multipleStudentModel);
}
}
溶液:
---------- update single student's view ---------------
Student (name=s1, rollNo=1)
---------- update multiple students' views ---------------
Student (name=s0, rollNo=0)
Student (name=s1, rollNo=1)
Student (name=s2, rollNo=2)
Student (name=s3, rollNo=3)
Student (name=s4, rollNo=4)
代码结构:
-src
├ view
│ ├ MultipleStudentView.java
│ ├ SingleStudentView.java
│ └ StudentView.java
├ model
│ ├ MultipleStudentModel.java
│ ├ SingleStudentModel.java
│ ├ Student.java
│ └ StudentModel.java
├ controller
│ └ StudentController.java
└ Main.java
这些都是这里的代码,希望有帮助〜
(顺便说一句,在典型的JavaEE开发中,模型只是POJO,而视图更常见的是逻辑视图,如String。这只是MVC的一个例子,用于理解。)