java - 单个和多个对象的MVC模式的实现

标签 java design-patterns model-view-controller

我已经使用 MVC 模式实现了学生管理(教程:Link)。我决定将学生分为“SingleStudentModel”和“MultipleStudentModel”,但我不确定这对我的情况是否有意义。总的来说,我对我的解决方案不满意。是否可以用一个 Controller 处理单个学生和多个学生? 如果将模型导入到 View 类中是否可以(请参阅 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(模型)

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(模型)

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

学生 View .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);
    }
}

学生 View .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 的接口(interface),这样 MultipleStudentModelSingleStudentModel 类都将实现

所以你不需要定义两个基本上共享相同逻辑的 Controller ,这也可以让你在运行时使用 setModel 更改 Controller 行为

这还将创建一个契约(Contract),将您的模型绑定(bind)到特定的方法,如 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 类来完成此操作

also, basically one student is just a list of students contains only one element, you can remove that redundancy

关于java - 单个和多个对象的MVC模式的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50074616/

相关文章:

java - Tesseract - 找不到指定的模块

java - 在Java中实现CustomList的List接口(interface)

java - 具有多个Windows设计的MVC

mysql - 404 未找到 :The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

html - Angular.js Controller 无法设置属性

Java方法帮助

java - 百家乐游戏中错误的附加值

c# - C# 中的类型调度与多态性。业务实体和非业务逻辑

java - 使类更通用

php - 为什么要使用匿名函数?