This is about a school assignment so I’m trying to do things by the book. I feel like I’m getting the grips of Java, but good programming practice, design patterns, etc. are all rather new to me.
I’ve made my model and it works fine. It contains a student class which contains a number of fields with student information (obviously). Now I want this information to be displayed in a couple of JLabels. It is easiest to pass a whole student object to the GUI and use JLabel.settext.getname() etc. a number of times and no doubt this will work perfectly. But I feel that the student object is part of the model and by passing it to the GUI I’m not using a MVC pattern anymore. Am I right here?
I did make a controller object for passing data to and from the model and the GUI but for passing only strings to the labels or setting the JLabel text through the controller, I need either a lot of setters in the GUI, or I would have to make all JLabels global fields which doesn’t feel good either.
Any advice on this?
the GUI should worry about all the interface stuff. I guess you have a class that is your GUI for doing ‘stuff’ to the student with your JLabels. Just pass your student instance to this class and let it do what it needs to do. When it is done it will call a controller method to do whatever needs to be done.
OOD deals with passing the objects around that you want to manipulate. You don’t need to break the objects apart for passing in MVC. You are supposed to pass that around really if this is a general case. The model defines the data objects that you will be working with… or more specifically the system will be working with (controller) and the users will be working with (GUI). These class are built to be passed around. You will have to do a lot more work if you un-encapsulate all the information hehe 🙂