This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes MVC Pattern ?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "MVC Pattern ?!" Watch "MVC Pattern ?!" New topic
Author

MVC Pattern ?!

Alex Qiu
Greenhorn

Joined: Jul 19, 2002
Posts: 24
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
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

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.
Alex Qiu
Greenhorn

Joined: Jul 19, 2002
Posts: 24
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 .
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

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.
Alex Qiu
Greenhorn

Joined: Jul 19, 2002
Posts: 24
Thank you for your patient . Now I clear my confusion .
And also great thanks for this forum .
Chiji Nwankwo
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
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"
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Chiji Nwankwo
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Thanks for your reply Mark.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MVC Pattern ?!
 
Similar Threads
on UI design :(
Question about MVC in Swing application
MVC structure
Model View Presenter pattern
MVC or Moderate pattern for GUI?