samedi 28 avril 2018

Implementation of MVC pattern for single and multiple objects

I've implemented a student administration with MVC pattern (Tutorial: Link). I've decided to divid the students in "SingleStudentModel" and "MultipleStudentModel", but I'm not sure if that make sense in my case. In general, I'm not satisfied with my solution. Is it possible to handle single student and multiple students with one controller? Is it ok if a model import into a view class (see StudentsView.java)?

How can I improve this project?

Thanks in advance.

Student.java (Model, data class (?))

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

            }
      }

Aucun commentaire:

Enregistrer un commentaire