After I read a lot of thread here and the article that Mark recommend , http://ootips.org/mvc-pattern.html I just sum up my understanding here , and see if it is right or not . As a MVC Pattern , it contains Model , View and Controller 3 parts . the Controller knows and helds the references of the View and Model . the View helds a reference of Model and a weaker pointer of the Controller (Maybe one of the interface it implements ). the Model helds a weaker pointer of the View so that it should not call anything more than that . Now in the real implement , I want to set the main frame as the controller , the Jtable as the view (Actually the model in the Jtable) , and the DataService as the Model . Is that right ? so in the frame , it helds the reference of the Jtable and the DataService . the JTableModel helds the DataSerice . But the question is , why the Model helds a weaker pointer of the View ? And also why the view helds the weaker pointer of controller ? It seems that it refers to another Design Pattern , Observer . Like the Architect in Swing , addXXXListener(). Is that shows up the MVC pattern includes the Observer ? It confused me late in last night ... would you pls kindly show me some light ? Thank you in advance .
Nothing is impossible to a willing heart .<br />----------------------------------<br />SCJP SCJD
But the question is , why the Model helds a weaker pointer of the View ? And also why the view helds the weaker pointer of controller ?
I am not sure what you mean by "weaker pointer" (I assume you mean loose coupling between the classes), but the idea is to make all 3 components of the MVS as independent as possible, so that each component can evolve without affecting the others. Yet all three components interact as a whole, so some dependency is neccessary. The solution to this dillema is to make this dependency loose. For example, when the model data changes, you want that data to be reflected in the views, but you don't know in advance how many views you might have or what type of data might need to be updated in the views. In this case, you can define a collection of model listeners, as represented by a member variable in the model, and invoke some sort of update() method from the model on all model listeners. This will allow your model to update multiple views by sending weakly typed updates (for example, by sending data as parameters of type Object). Eugene Kononov.
Joined: Jul 19, 2002
Hi Eugene , what you assume is right . It has loose coupling , It says the Model should have a reference of the view's prototype (class type maybe one of the view's interface it implements) , so that the Model can call some of the Views' method , such as updateView() ... then each view can be updated when model changed . Just like you mention in some other threads , the fireModelChange() method in model will call each one of the view's updateView() method , now I come to sense . But it is just why the model should have a weaker pointer of the view . Still don't know why the view should have a weaker pointer of the controller ? Actually what is that use ? Is there some tricks inside ? Sorry for my stupid head , and Thank you in advance .
Joined: Oct 08, 2001
Still don't know why the view should have a weaker pointer of the controller?
I don't know where you read about it, but presumably the view "gestures" to controller about the user's intended action. For example, on the click of the "Search Flights" button, the GUI code may do something like this: controllers.searchFlightRequested() where controllers is a collection of view listeners. But it is also possible to implement MVC so that the view(s) don't know anything about the controllers (that's what Mark is advocating using the hook methods). But then, of course, you come back to the great question, how does the controller get the data from the view(s)? And we have discussed that on a number of occasions, but unfortunately have not found the solution that satisfies everyone. Eugene.
Joined: Jul 19, 2002
Thank you for your patient . Now I clear my confusion . And also great thanks for this forum .
Hi, I have noticed that alot of references have been made to the use the MVC pattern on the client side gui. Have you actually split the gui into a model, a view and a controller or are you referring to the way some of the swing components uses the pattern? Please explain. Thanks, Chiji
SCJP, SCJD, SCWCD<br />"Meekness is not weakness, but power under control"
To get the informoation from the GUI, you would need some method in the GUI that anyone from the outside that knows about it can call it and get the information. This will still keep the GUI from know anything about anyone else. No matter how we slice it, someone somewhere has to know about these objects, it could be your application controller, aka main program, or it can be an Action object, or it can be the controller. I like the controller, but as Eugene pointed out each one of us has different opinions on this. That is fine and you get to choice which one you like better and that you find easier to maintain and reusable. Yes we are talking about MVCs seperating our own code and also how Swing uses it, you need to know both. Mark