This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes doubts on MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "doubts on MVC" Watch "doubts on MVC" New topic
Author

doubts on MVC

Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
Hello All,
I am trying to design the MVC part of my project.
I use following structure:
View <-----> Controller <------> Model

I have the following classes:
Controller .
DataModel - this represents of logical model of the data and recieves the data from the Database.
The MainWindow - view provides all GUI-staff (buttons, text-fields, Table).

After the user startes the programm as remote-Client or local client and selectes the
necessary information(database location, or port, or IP) this information will be passed to MainWindow.
And the MainWindow initializes the Controller.
And Controller initializes than the DataModel.
After the controller calles initialization method of the DataModel, model gets with the help of the ConnectionFactory
the instance of the DataAdaptor (local or remote). And all DataAdaptor methods will be called from the DataModel.


(it's only sketch-code)

It works in such a way:

The user enters the search criteria and then presses the "find" button. This calles the method findByCriteria in the controller.
The Controller calles the correcponding method in the DataModel and DataModel recieves the data from
the database (DataAdaptor for local or DataAdaptorRemote for remote). And sents it to Controller. Controller creates the MyTableModel
and sents it to MainWindow.

My questions are.
Do I correct implement the MVC pattern? Are the interaction correct?
Am I missing something?

Or is it correct to have a controller that has DataModel and View objects?
Thanks for you comments!
Regards,
Olena

[ March 28, 2005: Message edited by: Olena Golub ]
[ March 28, 2005: Message edited by: Olena Golub ]

SCJP 1.4<br />SCJD 1.4 (in progress)
kalyan kumarG
Greenhorn

Joined: Mar 28, 2005
Posts: 7
what you have created is good and it is the basic besign of MVC.<br/>
avoid hardcoding the connections among the MODEL, VIEW and CONTROLLER.<br/>
Bring it outside of the code.
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by kalyan kumarG:
what you have created is good and it is the basic besign of MVC.<br/>
avoid hardcoding the connections among the MODEL, VIEW and CONTROLLER.<br/>
Bring it outside of the code.


This is a good suggestion. I used the Observer pattern to allow any interested objects (such as the Views and Controller) to register their interest with the Model and be informed of changes to the Model. I also had the Controller build the Actions and provide a way for different Views to get references to those Action objects.
[ March 28, 2005: Message edited by: peter wooster ]
Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
Thanks for your comments.

But I am still confused. Do I need to implements this Observer pattern?
Do I recieve a minus points implements in a way that I described?

I will try to implements this with observer but I am not sure if I can this. I am not very familiar with this pattern

regards,
Olena
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi Olena,

do you have the design patterns book Head First Design Patterns?

I love each pattern described in this fantastic book, it's Head First .

Regards,
Darya


SCJP, SCJD, SCWCD, SCBCD
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Olena Golub:
Thanks for your comments.

But I am still confused. Do I need to implements this Observer pattern?
Do I recieve a minus points implements in a way that I described?

I will try to implements this with observer but I am not sure if I can this. I am not very familiar with this pattern

regards,
Olena


The Observer pattern is very simple to implement, and you already know how to use it. What you need is to add 2 methods to your model
- addModelListener
- removeModelListener
These manage a collection of interested listeners (observers). When something happens that is interesting you iterate through that collection and run the methods in an interface they implement.

Define an interface called ModelListener that contains methods such as
- dataAvailable
- stateChanged

In your View and Controller, implement ModelListener or use anonymous inner classes that do, and call the addModelListener method on the Model to register interest.

This sort of thing helps you get good marks for OO design.
Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
Hi Peter,
thanks for your help!


In your View and Controller, implement ModelListener or use anonymous inner classes that do, and call the addModelListener method on the Model to register interest.


To implement the ModelListener in my View and than called addModelListener on the Model means that my View should have an
instance of the model. Am I right? But I don't want that view knows something about my model.
Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
what about this?
If i have two Listeners: one for the model and one for the view.
If the view is changed, controller says it to the model. Model recieves the data and notifies the Controller. Controller than passes this data to view.

Only the change that i made: view will be initialized in the controller.
This means my View and Model don't know about controller.
Is is correct implementation of MVC and Observer? Or do I still need to implement only one Listener and implement it in view and controller?



[ March 28, 2005: Message edited by: Olena Golub ]
Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
As I found out my second implementation that I showed above is not direct MVC but derivation that called Model-View-Presenter.
Can I use this pattern? What should I write about my GUI in my choises.txt that I use one kind of MVC or that I use MVP pattern and no words about MVC?
How do you think is my implementation correct?
Please comment it!
I am very thankful for your comments!
[ March 30, 2005: Message edited by: Olena Golub ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doubts on MVC