MVC: Best Approach?
Hi,
I have been working on this assignment for some time however I have arrived to a point were I am changing the code of my application without arriving anywhere!
I am following the MVC rules as found on some tutorials on the internet. My Model is made from an abstract class called Animal which is then inherited by other classes such as Dog, Cat, Cow, etc.
I made the Animal abstract class extend the Observable class (java.util) so that the class could be viewed from the view classes. In each set method of the class I call the notifyObservers() method. However this (in my opinion) has a drawback because during one single save (which calls several set methods) the view gets refreshed several times!Is there something I can solve this?
My second problem relies with the view themselves. As I have stated above I have an abstract class called Animal, and several sub classes. I decided to use the same approach for the view as well. I created an abstract class AnimalView and then extended this from DogView, CatView, etc.
Now this part was very confusing to me!How best to bind the model with the view? For this I created two methods in my view classes. The first one is called updateModel() and the second one is called updateView().
The updateModel() is called to update the model with the details inside the view. This calls several set methods of the model object. On the other hand the updateView() will update the view with the contents in the model.
However with the Observer interface this seems impossible, when the model is changed the Observable will call the view, however it will never call the updateView() of the Animal class, since there is another updateView() method in the DogClass. I can call the updateView Animal class by calling it from the DogClass (super.updateView) but I am starting to see my application being a little confusing!!
As you can see, each animal I have stored in my application, will only have one view at a time, so the whole MVC seems to be useless!! It is useless to update the view when the model is changed with the details in the view! I also have a list with all the animals in it, however the list gets refreshed automatically each time I save the animal (without I have placed any extra code!) and so I am starting to wonder whether to scrap all the MVC code! Something I do not wish to do because I want to learn it this MVC!
Anyways, do not want to annoy anyone with a long post! I would be thankfull for any comments :) which would help me a lot to further understand how the MVC works in practice rather then just in theory!
Regards,
sim085

