Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More for Confused with MVC

 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all,
I start a new topic as the extention of topic "confused with MVC".
Hi, Eugene,
In the code you provided, the controller contains both view and data as instance variable. I think this is one type of "MVC". Here is my comments:
1. Sun proposes "MVC" basically for J2EE. JSP is View, Servlet is controller, JavaBean is data. JSP calls Servlet, Servlet calls on JavaBean and do some business logic, and then sends result back as JSP.
Pls notice:
1) servelt needs not contain a View as a instance variable. 2) servlet may disappear once its service finishes, if not using servelt pool. The benefit, also one of the key merits, is that the controller(servlet) can handle multiple views(JSP).
http://java.sun.com/blueprints/patterns/MVC-detailed.html
2. I know J2EE is different from J2SE. But it seems controller does not have to be there all the time. The key point is the seperation of M.V.C.
I also see some drawbacks in the code you presented:
1) controller is not "reuseable" as it is just specific to mainView and flightModel. What if we want controller to handle more views?
2)

For example, you can replace JTable in the GUI with some other widget, and not a single line of code needs to change in Model or Controller.

what if change mainView.getDestinationAirport to another name, then the controller code should also be changed.
Any comments/suggestion?
Shan


possed by Eugene Kononov
// Contract between the Model and its listeners (Views), -- Observable-Observerpublic interface FlightsModelListener { public void modelChanged(int notificationType, Object data);}// Modelpublic class FlightsModel { public FlightsModel() { listeners = new ArrayList(); } public void fireModelChanged(int notificationType, Object data) { int size = listeners.size(); // notify all registered listeners about the change for (int i = 0; i < size; i++) { ((FlightsModelListener)listeners.get(i)).modelChanged(notificationType, data); } } public void addListener(FlightsModelListener listener) { listeners.add(listener); } public void removeListener(FlightsModelListener listener) { listeners.remove(listener); } public void searchFlights(String origin, String destination) throws DatabaseException, DataServiceException { String[][] searchResults = dataService.searchFlights(origin, destination); fireModelChanged(NotificationType.MODEL_DATA_CHANGED, searchResults); }}// Viewpublic class MainFrame extends JFrame implements FlightsModelListener { // All GUI widgets are private private JButton searchFlightsButton = new JButton("Search Flights"); private JButton searchFlightsButton = new JButton("Search Flights"); private JComboBox originAirportCombo = new JComboBox(); private JComboBox destinationAirportCombo = new JComboBox(); public void modelChanged(int notificationType, Object data) { switch (notificationType) { case NotificationType.MODEL_DATA_CHANGED: String[][] model = (String[][])data; // table data model is know to the view only and is not exposed tableDataModel.setData(model); break; } } // The callback method, -- Mark's idea public void searchFlightsAction(ActionListener action) { searchFlightsButton.addActionListener(action); } // The accessor methods public String getOriginAirport() { return originAirportCombo.getSelectedItem().toString(); } public String getDestinationAirport() { return destinationAirportCombo.getSelectedItem().toString(); }}// A simple utility class to hold the ids of eventspublic class NotificationType { public final static int ORIGIN_AIRPORTS = 0x0A; public final static int DESTINATION_AIRPORTS = 0xDEA; public final static int MODEL_DATA_CHANGED = 0xDeCa; public final static int SCHEMA_CHANGED = 0xCeCa; //private constructor for non-instantiability private NotificationType() {}}// Controllerpublic class FlightsController { private MainFrame mainView; public FlightsController(MainFrame mainView, DbConnDialog dbConnView, FlightsModel flightsModel) { this.flightsModel = flightsModel; this.mainView = mainView; this.dbConnView = dbConnView; mainView.setVisible(true); registerListeners(); } private void registerListeners() { mainView.searchFlightsAction(new ActionListener() { public void actionPerformed(ActionEvent e) { String origin = mainView.getOriginAirport(); String destination = mainView.getDestinationAirport(); searchFlights(origin, destination); } }); } private void searchFlights(String origin, String destination) { try { flightsModel.searchFlights(origin, destination); } catch (DatabaseException dbe) { Messenger.showError(mainView, dbe.getMessage()); } catch (DataServiceException dse) { Messenger.showError(mainView, dse.getMessage()); } }}
 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry the quatoed code is not clear. Please check the original at:
http://www.coderanch.com/t/183051/java-developer-SCJD/certification/Confused-MVC
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, you can't compare J2EE against J2SE. Simply because the seperate tiers of J2EE, make MVC a different beast. In J2EE, the Application Server is where you will find the Model and the Controller. The client should only have the View part. So therefore the Model is actuall on the Back end the database, and the Controller, the EJB gets the Model and sends back the information to the View on the client side.
In J2SE, all the components end up on the client side. So you have the View, Model and Controller all on the client side. Now the Model might originate from the Server side, but still ends up on the client. So you MVC design will be different.
Sorry I'll stop now, I started babbling at the end there and not even sure if I am right in those statements at the end.
Mark
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the code you provided, the controller contains both view and data as instance variable.

The controller holds references to Views, yes. You can actually make this relationship even weaker, if you want to. I am not sure what you mean by "data as instance variable". If you mean reference to the Model, then yes.

1) servelt needs not contain a View as a instance variable.

Ok, but somewhere you still have a controller that decides which JSP to dispense.

2) servlet may disappear once its service finishes, if not using servelt pool.

I am not sure what you mean. Once init()ialized, the servlet never goes away, unless explicitely destroyed.

2. I know J2EE is different from J2SE. But it seems controller does not have to be there all the time. The key point is the seperation of M.V.C.

I am sorry, but I don't follow you. What do you mean, "the controller does not have to be there all the time"?

1) controller is not "reuseable" as it is just specific to mainView and flightModel. What if we want controller to handle more views?

True to some extend. It is actually reusable, -- all you do is to pass a new View to the controller constructor. But I would agree, it's probably not the best way to handle it. As I mentioned in the beginning, you can make the Views-Controller relationship weaker.
what if change mainView.getDestinationAirport to another name, then the controller code should also be changed.

Change the public method name? Yes, of course the users of that class would need to change, too. But this is as expected of the public interface.
Eugene.
[ April 15, 2003: Message edited by: Eugene Kononov ]
 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Eugene,
Thanks for your reply.

am not sure what you mean by "data as instance variable". If you mean reference to the Model, then yes.

Yes, I mean Model. Sorry for this.


Ok, but somewhere you still have a controller that decides which JSP to dispense.

If my remory is correct, JSP is changed to Servlet first, then do the rest.
Eugene, your comments are right. To be direct, what I want to say is that MVC might be implemented in multiple ways. One is that Container contains both Model and View as instance variables, as you proposed. It is very good. The other, I am not sure proper or not, is that View contains Controller, and Controller contains Model, like a chain or like JSP-Servler-EJB.
Can we still call it MVC?
shan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic