aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes FBN: MVC pattern  without Model Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "FBN: MVC pattern  without Model Class" Watch "FBN: MVC pattern  without Model Class" New topic
Author

FBN: MVC pattern without Model Class

Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
I have a view class and controller class, for a screen that prompts user to select connection mode(from combobox), and allow him to enter DNS name and port no to connect to system. This screen(JFrame)
does not have a Model class . Controller class has reference to View. Is this MVC pattern or not ? Do we always need a Model class, View class and Controller class in MVC pattern ?
Do i need a Mediator class to create Model, View and Contoller object and set up their relationship ? Or Contoller class can do this job ?
Regards,
Akash
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11527
    
100

Hi Akash,
Strict MVC does require all three participants.
The usual variation on MVC is to remove the controller - the MVC without controller is often called Document-View pattern.
The question you have to ask yourself is whether it makes sense to use MVC in this case or not? Can you see a use for abstracting the data into a model? If not now then in the future? If you can see a use for MVC, then by all means, use it. But trying to force a pattern onto a solution can be worse than not using a pattern at all.
Do i need a Mediator class to create Model, View and Contoller object and set up their relationship ? Or Contoller class can do this job ?

The controller is quite often the mediator between the view and the model.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
Thanks Andrew.
I thought that View-Controller may be necessary component in MVC.
Below i have a put method of my view class. I have a conroller class also.
I do'nt know what would go into model from this class, and secondly who will contol action on view without contoller. Because typlically controller acts and view listener.
This view class makes a screen that allow user to select connection mode, and allow him to enter DNS name and port no.


Reagrds,
Akash
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Hi Akash,
I generally model my Model class a a simple JavaBean that contains all of the information I need to display. I leave the displaying of that information to the View. Thus, you view might recieve a simple bean, and based on the information in that bean, construct a JPanel, etc.

HTH,
M


Java Regular Expressions
Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
Thank You Very Much, Max Habibi.
Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
Max and Andrew,
I want to access the value which user has entered on the Connection screen.
I have added hook method for the button in view class. That i call in Controller class to add listener to view elements. If user presses OK button, i have to access values (like connection mode, dns name, port no) which user has entered to bring booking screen. I need to validate the values to check whether server is running on the given host/&port or not.
Can Controller(it has referenec of View) class can access it from View, using get method, which i can provide ?
What is the write way based on MVC pattern to access the values entered by user on screen in the Controller ?
Regards,
Akash
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Hi Akash,
I would probably capture the user's input as a JavaBean(as I mentioned above), and persist the state of that bean a property file. That way, the next time the user logs on, you can offer to default to the values they used last time. Do a search here, or @ the Sun site, for the preferences API.
M
Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
Max,
This is nice idea, i will try this.
How should i first time access user entered values in controller( when user presses logon button). Should i directly access these values( from get methods of View for fields )inside controller ?

Regards,
Akash
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11527
    
100

Hi Akash,
If you are storing those values in a bean, then you would probably use that bean elsewhere in your application - you would not refer back to the original values in the View. Since the View is no longer used, you might want to free up resources by disposing of the View.
Alternatively if you decided to have this information in a model, you could make the model a singleton, and have other classes get the static data from that model when they require it.
Regards, Andrew
Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
Thanks, Andrew.
Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
Hi Andrew,
Are "bean" and "model" are two different objects , or same concept; only difference in saying.
I was reading Max book, what i understood that model is a contract between view and controller. That passes back and forth between view and controller, as required by view/or controller.
So is your "bean" same as "model" in MVC, or is it something different? I am confused now with the term "bean" in MVC pattern.
Regards,
Akash
Vlad Rabkin
Ranch Hand

Joined: Jul 07, 2003
Posts: 555
Hi guys,
Andrew:
Strict MVC does require all three participants.

My View is a bit mixed with Controller:
I have Handlers(which are excially controllers) inside ClientWindow class:

and a separate GUIController class, containing such methods, like book and find.

My GUIController class doesn't have any refence to the view (MainWindow or JTable, because all info needed from view is used in Handler.
I have done it in the way very close to Max book (except I updated TableModel not in Handler, but in GUIController).
Is it Ok?
Tx,
Vlad
Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
Hi Vlad,
You are correct. Your SearchHandler is basically event handler for the button, that you can have in view. But actual business logic will be in the
controller that performs command for view. Having SearchHandler in view means it has capabality to send command to controller like hey give me this.
And controller will process view's command and may return a model object to view. Same like Fast food example in Max book.

Regards,
Akash
Vlad Rabkin
Ranch Hand

Joined: Jul 07, 2003
Posts: 555
Hi Akash,
The problem that I am worried about, is that in one discussuion, somebody said that Contoller MUST have a reference to the view.
I thought that good MVC should decouple View from Controller, what I have done.
My View creates GUIContoller object.
My GUIContoller creates table Model object.
my view has reference then to the controller and get references to the Table model througg the GUIContoller.getTableModel() method.
So, My contoller doesn't have any reference to the View.
Are you sure it is Ok?
Tx,
Vlad
Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
In my opinion, if you create your controller, view through some other class say application launcher applying mediator pattern which i would do, you do need reference of View in Contoller.
Actually controller should not know about view. Its like counter clerk that receives the order from different customer; does he need to know who customer is?
I think, NO. Counter clerk job is to process the command asked by cutomer(View). But, You(view) need to know counter clerk(Controller) to place the order.
My entire understanding is based on Max book. I like that book very much.
The problem that I am worried about, is that in one discussuion, somebody said that Contoller MUST have a reference to the view.

I do need think so, in true MVC.
I thought that good MVC should decouple View from Controller, what I have done.

Yes.
My View creates GUIContoller object.

I would prefer, let some other class create view and controller object.
In real world also these are created by some one else .
My GUIContoller creates table Model object.

Thats fine.
my view has reference then to the controller and get references to the Table model througg the GUIContoller.getTableModel() method.

Thats fine.
So, My contoller doesn't have any reference to the View.

Thats fine.
Are you sure it is Ok?

Yes.
Vlad Rabkin
Ranch Hand

Joined: Jul 07, 2003
Posts: 555
Hi Akash
I would prefer, let some other class create view and controller object.
In real world also these are created by some one else

Yeas, that's right. My input in previous mail was wrong.
Another class of mine creates controller, calles setConnection() on it (to get connected to the database and than creates View and puts contoller object in C-tor of View:


So, everything seems to be good.
Tx!
Vlad
[ September 26, 2003: Message edited by: Vlad Rabkin ]
Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
Hi Vlad.
Thanks. So we are the same page. I would do in same way. But still i am confuse with the term "bean" in MVC pattern, as mentioned by Andrew. Do you have any idea ? I am waiting for Andrew's answer also.
Regards,
Akash
Vlad Rabkin
Ranch Hand

Joined: Jul 07, 2003
Posts: 555
Hi Akash,
I was trying to avoid answering this question, since the GUI in this assignement is my first GUI I have ever done. So, please, don't be angry if I will tell you something wrong.
JavaBean to my opinion can be considered (as often in jsp) as a simple object containing private variable with get/set public access methods. No other logic.
Let's take an example.
Your client needs properties for connecting a database. Let's take a remote db sample. It is then ip and port.

Now, how usually looks your model? Your model usually is also the class containing get/set methods.
As I understand words of Max that your properties (java bean) to log on can be used as a model.
As far as I understand Andrew, he means this model will be accessed by different views you can make this bean/model as Singleton. Example:
Without Singletone:
View 1 - ConnectionProps 1
View 2 - ConnectionProps 2
View 3 - ConnectionProps 3
where you ConnectionProps is actually the properties shared by all view (the data inside are equel).
With Singleton:
View 1 -\
View 2 --- ConnectionProps 1
View 3 -/
In this case you can safe memory resources.
I, personally, use it only in one view, My GUIClientStart dialog. And I do it in so called bean/model mannerI make the configuration persistant I use Properties class:

public class ConnectionProps{
private java.util.Properties props = ...
public ConnectionProps(){
...
}
public void setIp(String port){
props_.setProperty("client.serverport", port);
}
}

That is what I do (and that is what I guess Max ment).
Your View (Log On dialog) gets input into this ConnectionProps, which is actually your Model! You can then instruct GUIController to save the bean (props.store(...)) in the properties file or access it.

Ups... I am not sure I am answering your question. Let me know if it is what you are asking for and if it is what Max and Anrew where trying to explain.
Please confirm if it helps, if not could you ask me again?

Best,
Vlad
Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
You are right. You have cleared my confusion.
We use bean term more in JSP. This bean is kind of model in JSP. Why i have written kind of model in JSP; because in Swing model for ex AbstractTableModel class , it also has addModelListener method. So this model(in Swing) allows to become observable. But in bean, typically we do not see this method. Am i right ?
I my posting "posted September 26, 2003 11:35 AM" in this thread. I made some major typo, that contradicts my answer. I missed "not" word in two sentences, but thanks that you understood it.
Below I have inserted "NOT" word in those two sentences.

1. "In my opinion, if you create your Controller and View through some other class say "ApplicationLauncher", applying Mediator Pattern which i would do; you do NOT need reference of View in Contoller."
2. I do NOT need think so, in true MVC.

Thanks and Regards.
Akash.
Vlad Rabkin
Ranch Hand

Joined: Jul 07, 2003
Posts: 555
Hi Akash,

So this model(in Swing) allows to become observable. But in bean, typically we do not see this method. Am i right ?

I guess so.
I have never worked with JavaBeans for GUI. I used always term "bean" for
classes without logic (only setters and getter) in jsp and "enterprice bean", which is irrelevant to this discussion.
I feel really unsafe to advice you, because my answers can be incorrect
Let's wait a bit for Andrew or other GUI gurus ...
Best,
Vlad
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FBN: MVC pattern without Model Class