views:

72

answers:

3

After writing a few lesser programs when learning Java the way I've designed the programs is with Model-View-Control. With using MVC I have a plethora of getter methods in the model for the view to use. It feels that while I gain on using MVC, for every new value added I have to add two new methods in the model which quickly get all cluttered with getter & setters.

So I was thinking, maybe I should use the notifyObserver method that takes an argument. But wouldn't feel very smart to send every value by itself either so I figured, maybe if I send a kind of container with all the values, preferably only those that actually changed. What this would accomplish would be that instead of having a whole lot of getter methods I could just have one method in the model which put all relevant values in the container. Then in the view I would have a method called from the update which extracted the values from the container and assigning them to the correct fields.

I have two questions concerning this.

First: is this actually a viable way to do this. Would you recommend me doing something along these lines?

Secondly: if I do use this plan and I don't want to keep sending fields that didn't actually change. How would I handle that without having to have if statements to check if the value is not null for every single value?

A: 

If you have too many getter it's ok. But you shouldn't need the setter. The view is supposed to only read/query the model.

The MVC pattern should promote something that is asymmetric: the control update the model by calling methods in the model that embed the logic and update the sate accordingly; this respects encapsulation. The view reads/queries the model via the getters. This goes a bit against information hiding, but that's how MVC works.

I wouldn't personally pass all information in the events. It sounds complicated to me: either you end up with something that is not statically typed (e.g. you pass hashmaps), or with a plethora of typed events. I would stick with something simple, and have (possibly many) getter in the model.

ewernli
I'm sorry I was a bit unclear about the setters. I have the setters to be called from the control and that I see as the only way. But I don't quite like that I have to call that many getters all the time.
dege
The view need to display the model. You inherently need getter to access the information to display in this case. To me this justify the existence of these necessary getters. I feel like an attempt to remove the getters will result in something more complicated than the mere presence of getter. You asked for a recommendation, so here is mine. But let's see what others say.
ewernli
A pragmatic discussion about getters: "Getter Eradictor" http://martinfowler.com/bliki/GetterEradicator.html
ewernli
A last comment: you can of course pass tiny bit of information in each event. But as I wrote, you end up conceptually with DTO, which means either many classes *or* untyped structure. This is sometimes necessary if you have network boundary between the presentation and the model, and this works. But this is *more complicated* than having getters.
ewernli
+1  A: 

I've more familiar with the MVP paradigm, but hopefully they're similar enough to comment. While getters (and setters) in and of themselves are not necessarily evil, they are sometimes a sign that your subsystems are too strongly coupled. One really great way to decouple this is to use an event bus: see Best practices for architecting GWT apps. This allows the view to just shoot off events for the controller to listen for whenever something important happens, and the view can listen for events whenever something changes in the model that corresponds to updating the view. Ideally you wouldn't even need to ever pass the model to the view, if you can break up any changes into incremental pieces and just tell the view to change this part and then this other part.

Steve
+1  A: 

If you feel you have too many getters (and setters) in your model class, maybe you have too many fields altogether. Is it possible that there are several distinct classes hiding within your model? If you extract these into separate classes, it may make your model more manageable.

OTOH the associated container you are thinking about could also be viable - but then why duplicate all data? You could instead use the associated container directly in the model to store all properties you can think of. And you can also pass this around for observers to get updates (preferably wrapped into an unmodifiable container, of course) - although in this setup you wouldn't need to.

In general, Java is a verbose language which expects you to put all those getters and setters (and a lot more) in place. However, any decent IDE can generate those for you with a few keypresses. Note also that you need to write them only once, and you will read and call them many many more times. Verbose also means easily readable.

Péter Török
Even while the model is split between several classes I have one huge view. Possibly I could have several views, like a lesser one extending JPanel and adding itself to the mainView/container.
dege
@dege not sure about that one - I feel it would overcomplicate the view in the long run. But experiment with it if you feel it's viable - only you can decide whether it works for you. With this little information I have about your case, I certainly can't.
Péter Török