• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mvc question

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, ranchers, there's one thing here i cant work out:
when the linstener method in the controller revoked, how does it get the component value stored in the gui , does the gui provide the public field or method for the controller to do that ?
Regards
James
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The controller knows about the GUI, so therefore can get any value that is on the GUI directly.
Mark
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The controller knows about the GUI, so therefore can get any value that is on the GUI directly.


Mark,
I was wondering if there is a cleaner way to do this, for example defer knowledge of GUI values to a Mediator. So the controller job is to process the events, and if controller needs to know about some GUI values, it will ask the mediator to get those values. So in the controller, you can do something like this:

Eugene.
 
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eugene,
Controller is the mediator between the GUI and the rest of the RMI world. There is no need to have another mediator between the controller and the GUI. When you use the Mediator pattern it is always preferred to have a tight coupling between the controller/mediator and the GUI. In this case, I recommend keeping all the members of GUI private. Controller can access the values in GUI only using accessors.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Sai wrote:
There is no need to have another mediator between the controller and the GUI.


I misnamed Mediator, -- it should actually be Model, for a complete MVC pattern:
-- In GUI, all controls are private, it knows nothing about Controller, and keeps a reference to Model. When GUI controls change, GUI updates the Model. GUI actually implements Observer, so that it can update in response to Model change. For example, if in the "Seats To Book" text field the user types a number greater that the number of seats left in the table, the model can set its boolean field bookingAllowed" to false. This will update the GUI (make the "Book Flight" button disabled).
-- Controller only calls the public methods of GUI to register the action listeners, it knows nothing else about GUI. If controller needs some values from the GUI, it would call the corresponding accessor methods in the Model (not the GUI). If instead you call the public methods of the GUI to get the values, your controller is too tightly coupled with the GUI.
-- Model holds all the values and has a set of get/set for each control. Model extends Observable, and notifies the GUI of the changes in the model.
How's that?
Eugene Kononov.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Model is your access to the Data class.
Mark
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eugene,
Looks good. I didn't do it this way and I don't know if anyone has done it. Having a tight coupling between the controller and the gui is fine and that is how the mediator pattern is supposed to work.
Also in the mediator pattern, it is preferred to have the controller get notified about changes to the gui values. In your case, your model is getting notified instead of the controller.
I don't know how the grader will look at it. I guess you have to justify for the extra Model class instead of keeping the model in the controller.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Mar wrote:
Model is your access to the Data class.


Actually no, the model just holds the state of the application (origin airport, destination airport, seats to book, etc). And it has setters and getters to manipulate the application state.
The access to Data class is provided to Controller by what I call a FlyHighDataService class (wich I consider a facade to data aceess).
So, my book() method in Controller looks like this (notice that controller actually holds reference to table model and application model):


void bookFlight() {
String flightNumber = flyHighModel.getFlightNumber();
int seatsToBook = flyHighModel.getSeatsToBook();
String confirmationMessage = "You are about to book "
+ seatsToBook + " seat(s) on flight " + flightNumber
+ ". Proceed?";
int response = Messenger.showConfirmation(confirmationMessage);
if (response == JOptionPane.OK_OPTION) {
try {
int seatsLeft = dataService.bookFlight(flightNumber,
seatsToBook);
tableDataModel.setData(row, 8, String.valueOf(seatsLeft));
String successfullBookingMessage = "You have successfully booked "
+ seatsToBook + " seat(s)"
+ " on flight " + flightNumber + ".";
Messenger.showInformation(successfullBookingMessage);
}
...
}
}


This might be a bit of an overkill for this assignment, but after I coded this, the MVC benefits became very apparent to me.
Eugene.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn, I used quote instead of code, let me try again:
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually Yes and no, because in your case, where do you get that data? from the Data class. But the Model is what the data that is currently showing in your GUI, So it is both.
Mark
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Actually the on getting some cue from the GUI (View) the controller updates the Model. The controller acts as a a mediator between view and the model, thus mediator pattern.
The moment the model changes its state the view comes to know about it, as it has subscribed itself as an observer. thus, Observer pattern.
you ofcourse need not implement these patterns urself as they are inbuilt into the swing components
Regards
Arup
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Actually the on getting some cue from the GUI (View) the controller updates the Model.


I don't understand what you are saying here. The question is, if controller needs some GUI values before it processes some action, how does the controller get those values without referencing GUI?
For example, if the user changes the selection in the "Destination Airport" combo box to "Mumbai", and then clicks on the "Search Flights" button, the controller can process the event (search the flights). But before it does, it needs to know the destination airport. And we don't want to see something like this in our controller:

To avoid this in my Controller code, I make a callback to an updateModel() method that is defined in GUI. Over there, I update the model with selected origin and destinations. Now, the controller can get these values from the Model.
However, I don't like this implementation either, as now GUI knows about the model and can manipulate it bypassing the controller.
Any other solutions?
Eugene Kononov.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic