MVC: info on update method

Hi,

I'm using MVC pattern.

I've understood it, and I be able to use it, but I've some doubts.

Practically, in the view I have the update method;

Now, my view interact with many models, which advice it by executing his update method.

But in this update method, I've various pieces of code;

one for the update of the first model;

one for the update of the second model;

etc.....

I don't like this, because I want that,

when a model is updated, it advice view by executing only the corresponding piece of code.

Is there any other solution?

Excuse me if it's not clear.

Thank you in advance

MargNat

[691 byte] By [MargNata] at [2007-11-26 17:50:23]
# 1
No alternatives?
MargNata at 2007-7-9 5:02:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

I take it from your message that you're implementing a Swing-based MVC GUI (things would be hardly different for a Web MVC application).

In this Swing GUI, the View is an Observer of the Model, hence your update() method, correct?

Indeed you don't have to apply this blindly : you may very well have each model have its own Observer interface; indeed in the Java world, an Observer is usually implemented as a "Listener" idiom, which provides more strongly typed notification methods.

So you can have your ModelA notify a (set of) ModelAListener object where ModelAListener is an interface providing a modelAUpdated(ModelA model), same for ModelB and ModelC.

Then your view ony has to implement ModelAListener, ModelBListener, ModelCListener, and provide model-specific code in each model-specific method modelAUpdated(ModelA), modelBupdated(ModelB), modelCUpdated(ModelC).

SO when the ModelC instance is updated, it invopkes it's listener's modelCUpdated(ModelC) method, which only takes care of ModelC's changes.

All 3 methods could even have the same name modelUpdated(...) or update(...), with a different type of parameter each, the overloading specifying which Model has changed (but IMHO this would go against clarity of the code).

jdupreza at 2007-7-9 5:02:55 > top of Java-index,Other Topics,Patterns & OO Design...