aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes what should a controller do?? 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 "what should a controller do??" Watch "what should a controller do??" New topic
Author

what should a controller do??

biang lin
Ranch Hand

Joined: Jun 02, 2003
Posts: 91
Hi all,
Peoples said controller should be listening events.
So a controller should implements some listener interfaces,and all the user action (click,key press...) should be handled by the controller.
Am I right??


biang
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Basically yes. The controller is the source of the business methods/events of the view. The model represents the data.
The controller will run a method when the search button is pressed, or the book button is pressed on the GUI.
There are many ways of designing MVC that change who knows who, and who has a reference to who. But in general the Controller are the actions that the GUI needs performed.
There or thousands of posts in this forum on MVC, do a search on this forum and you will have mental fodder for ages.
Mark


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

Joined: Jun 02, 2003
Posts: 91
Thank you,Mark.
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
Hi Mark,
In my assignment, I have written two inner classes which handles action, and mouse events. Event handlers invokes the GUIController class, to get the data model, and any of the operations to invoke on database, GUIController class is responsible. I have written my own table model.
Can I say, my design follows MVC? Please guide me.
Regards,
Ganapathy.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

S. There are many ways to implement MVC, and who has references to who. It seems to be a more general question on which do you prefer, or is there a prefered way. Questions like, what does this mean if I happen to want to switch out the View, the model, or the controller. What kind of code changes will this incur, and is it major changes or minor, or in really decoupled space - no changes outside of the switched class.
I prefer the Hook methods which I have discussed in many threads that you can search on. This way I can swap out my View without changing any other code.
Mark
biang lin
Ranch Hand

Joined: Jun 02, 2003
Posts: 91
Hi Mark,
I'v read lot's of post you mentioned about "hook method",such as:
/**
* This method assigns the listeners to the objects on the gui that need listeners.
* It then maps the methods of this class that fire when the action occurs on the GUI.
*/
private void assignActionListeners(){
frontEnd.fileExitMenuAction(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
frontEnd.helpHelpMenuAction(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Documentation docs = new Documentation("clientDocumentation.html");
}
catch (Exception ex){
showErrorDialog(ex);
}
}
});
frontEnd.searchAction(new ActionListener(){
public void actionPerformed(ActionEvent e){
searchFlight();
}
});
frontEnd.tableAction(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
tableRowHasChanged();
}
});
frontEnd.passengersAction(new FocusAdapter(){
public void focusLost(FocusEvent e){
enableBookButton();
}
});
frontEnd.bookAction(new ActionListener(){
public void actionPerformed(ActionEvent e){
bookFlight();
}
});
}

But this piece of code is only for one single GUI,right?
So what if there are lot's of GUI?
We define lot's of methods like the method above for each GUI? Or just put all of the code into one method? Or there are lot's of controllers for each GUI??
Regards.
[ July 02, 2003: Message edited by: biang lin ]
[ July 02, 2003: Message edited by: biang lin ]
biang lin
Ranch Hand

Joined: Jun 02, 2003
Posts: 91
Take a look please.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Biang I am not sure what your question is.
The Hook methods are for the GUI class. A Controller can hook into these methods and register itself as a listener, and when the Action Event occurs a method in the Controller is run.
It makes no difference on how many GUIs you have. And it makes no difference with how many controllers you have.
Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Yes in my code that is for one GUI, but that doesn't meant that the Controller only controls one GUI, it could Control multiple GUI's if you want them too.
Mark
biang lin
Ranch Hand

Joined: Jun 02, 2003
Posts: 91
Originally posted by Mark Spritzler:
Yes in my code that is for one GUI, but that doesn't meant that the Controller only controls one GUI, it could Control multiple GUI's if you want them too.
Mark

Mark:
Thank you,your comment is very helpful for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what should a controller do??