tags:

views:

343

answers:

4
+4  Q: 

MVC in Java

Hi all,

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?

A: 

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 :)

Arthur Thomas
+2  A: 

Note that the Swing components all use the MVC pattern internally, so they already have a model. This is more relevant with complex widgets like JTable, where you definitely want your model to implement the TableModel interface.

The big question is how to reconcile your domain model with the internal models of the individual Swing components. One way to do this is to have setModel() and getModel() methods in your GUI class, which translate between them, i.e. setModel() takes your model and calls setText() on the individual JLabels, etc.

Michael Borgwardt
A: 

The view needs your model to create the UI (pull from model) and to receive updates from it (push by model). Ideally the model is presented to the view in read-only fashion. Controllers would provide the methods that the view will use to update the model, for a nice separation of concerns.

There are many different forms and interpretations of what MVC exactly is, try Googling it. Also, you can often find MVC on different levels in your application (e.g. Swing model, your domain model, ...).

With MVC and variants on MVC, the most important thing is that you can explain individual subpatterns (Observer, Facade, ...) and defend the design choices you made (advantages/disadvantages) instead of trying to implement the one and only unique MVC pattern, if it would exist.

So my advice for your assignment, if possible of course, would be to implement something that works well, inspired by what you've read on MVC and its subpatterns, and only afterwards see how well it agrees or disagrees with some "traditional" descriptions of MVC patterns you can find.

eljenso
That's all true, but don't forget that homework often is about your teacher and not about the homework. Sadly.
borisCallens
A: 

OK, thanks everyone.

So my advice for your assignment, if possible of course, would be to implement something that works well, inspired by what you've read on MVC and its subpatterns, and only afterwards see how well it agrees or disagrees with some "traditional" descriptions of MVC patterns you can find.

I will do that. It feels like its the best approach between rigid bureaucratic programming and doing everything 'quick and dirty'

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.

There's a certain logic to that ;-). At the moment I don't have a seperate class that displays the students information but that's added easily enough. I will also add some setmodel() and getmodel() methods to implement the needed structure.

Thanks for all the advice.

Berend Vervelde