samedi 20 février 2016

Implementation of MVC in Java

I've followed several guides on how to implement MVC in Java, and so far I've come up with the classes shown below. From my understanding, MVC follows these guidelines.

  • The Model should be independent and unaware of the View and Controller.
  • The View should wait for interaction from the user and inform the Controller.
  • The Controller gets and sets information in the Model and updates the View if necessary.
  • It's possible for the View to reference the Model. (This is what I don't understand).

Model:

public class Student_Model {

    private String fname, lname;

    public String getFirstName(){
        return this.fname;
    }
    public String getLastName(){
        return this.lname;
    }
    public void setFirstName(String newFname){
        this.fname = newFname;
    }
    public void setLastName(String newLname){
         this.lname = newLname;
    }

}

View

public class Student_View {

    public JPanel panel;
    public JTextField student_fname, student_lname;
    public JLabel stu_fname_label, stu_lname_label;
    public JButton changeName;

    public Student_View(){

        panel = new JPanel();
        panel.setPreferredSize(new Dimension(200,200));
        panel.setBackground(Color.red);

        student_fname = new JTextField(20);
        student_lname = new JTextField(20);

        stu_fname_label = new JLabel("First Name: ");
        stu_lname_label = new JLabel("Last Name: ");

        changeName = new JButton("Change student data");

        panel.add(stu_fname_label);
        panel.add(student_fname);
        panel.add(stu_lname_label);
        panel.add(student_lname);
        panel.add(changeName);

        panel.setVisible(true);

    }
}

Controller

public class Student_Controller implements ActionListener {

    Student_Model student_model;
    Student_View student_view;

    public Student_Controller(Student_Model sm, Student_View sv){
        this.student_model = sm;
        this.student_view = sv;

        initActionListeners(this);
    }

    public void actionPerformed(ActionEvent e) {
        student_model.setFirstName("Bill");
        System.out.println(student_model.getFirstName());

    }
    public void initActionListeners(ActionListener al){
        student_view.changeName.addActionListener(al);

    }

}

And finally it's put together in the as such:

public class MVC_Design_Demo extends JFrame {

    public MVC_Design_Demo(){
        setPreferredSize(new Dimension(500,500));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);
        getContentPane().setBackground(Color.black);

        init();

    }
    public void init(){

        Student_Model newStudent = retrieveStudentInfo();
        Student_View student_view = new Student_View();
        add(student_view.panel);
        Student_Controller student_control = new Student_Controller(newStudent, student_view);

    }

    public static void main(String[] args){
        MVC_Design_Demo mvc = new MVC_Design_Demo();
        mvc.setVisible(true);
        mvc.pack();

    }
    public static Student_Model retrieveStudentInfo(){
        Student_Model sm = new Student_Model();
        sm.setFirstName("John");
        sm.setLastName("Smith");
        return sm;

    }
}

Does this follow the MVC design pattern and are my above assumptions about the guidelines MVC should follow correct?

Aucun commentaire:

Enregistrer un commentaire