File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes about MVC and connection 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 "about MVC and connection" Watch "about MVC and connection" New topic
Author

about MVC and connection

xin dong
Ranch Hand

Joined: Mar 24, 2003
Posts: 33
I use MVC to design my client,but which class should include database connection?at first I think Model class will do,because it implements searchFlight and bookFlight method.but it seems Controller class is more adaptive.so I'm confused.or make a connection class alone?
any answer is appreciated.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Xin,
From Sun's design methodology blueprint describing MVC:
Model - The model represents enterprise data and the business rules that govern access to and updates of this data. Often the model serves as a software approximation to a real-world process, so simple real-world modeling techniques apply when defining the model.

Therefore, the model is the correct place to have your database connection.
Controller - The controller translates interactions with the view into actions to be performed by the model. In a stand-alone GUI client, user interactions could be button clicks or menu selections, whereas in a Web application, they appear as GET and POST HTTP requests. The actions performed by the model include activating business processes or changing the state of the model. Based on the user interactions and the outcome of the model actions, the controller responds by selecting an appropriate view.

So the Controller is used to pass the user interactions from the View to the class that is going to handle it. For the assignment, most of these interactions will be passed to the model, however they could go to other classes. E.g. if you have online help, then when the user clicks the help button, then the action is passed to the controller, which will then pass it to the classes that handle the online help.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Poorna Lakki
Greenhorn

Joined: Mar 04, 2003
Posts: 11
Originally posted by Andrew Monkhouse:
Hi Xin,
From Sun's design methodology blueprint describing MVC:

So the Controller is used to pass the user interactions from the View to the class that is going to handle it. For the assignment, most of these interactions will be passed to the model, however they could go to other classes. E.g. if you have online help, then when the user clicks the help button, then the action is passed to the controller, which will then pass it to the classes that handle the online help.
Regards, Andrew

Andrew,
Is it ok for the model to generate events for the view listeners. For example in one my views (Results screen) i initialize the results table data to zero rows when the application starts up. But as soon as the user queries for flights by using the search screen the model updates the results data with the new results and generates a event for the 'Results screen' to update its table model.
Thanks for ur time.
-Poorna Lakki
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Poorna,
Yes, this is desirable.
Normally what you have is one model. In the real world (not necessarily for this assignment) you may have multiple views for that model. Each view would normally send events to the model via it's own controller. If the model just "returned" data to the view via the controller, then the other view(s) will be out of sync.
The solution is the observer-observable design pattern.
The model is observable, and each view registers itself with the model as an observer. Whenever a controller sends an event to the model that modifies the current data set, the model will fire a changed event to each observer that registered with it.
There were some recent posts in this forum discussing limitations of the Observer interface & Observable class provided by Sun. They also discuss the whole MVC pattern fairly thoroughly. Try looking at MVC topic for more information.
Regards, Andrew
xin dong
Ranch Hand

Joined: Mar 24, 2003
Posts: 33
thanks for your answers ,I ve understand how to arrange it.
Poorna Lakki
Greenhorn

Joined: Mar 04, 2003
Posts: 11
Originally posted by Andrew Monkhouse:
Hi Poorna,
Yes, this is desirable.
Normally what you have is one model. In the real world (not necessarily for this assignment) you may have multiple views for that model. Each view would normally send events to the model via it's own controller. If the model just "returned" data to the view via the controller, then the other view(s) will be out of sync.
The solution is the observer-observable design pattern.
The model is observable, and each view registers itself with the model as an observer. Whenever a controller sends an event to the model that modifies the current data set, the model will fire a changed event to each observer that registered with it.
There were some recent posts in this forum discussing limitations of the Observer interface & Observable class provided by Sun. They also discuss the whole MVC pattern fairly thoroughly. Try looking at MVC topic for more information.
Regards, Andrew

Andrew,
Actually that post was the basis of my design. I got confused by ur first post where u explained everything in the MVC model except for the interaction between the model and the view. But it is clear now. Now i feel a lot confident about my design.
Thanks for ur comments.
-Poorna Lakki
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about MVC and connection
 
Similar Threads
Database connection
Servlets + database connection problem..
Any Client implementation without MVC
Connection Factory MVC
FBN: MVC pattern without Model Class