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

MVC

Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Just curious

Lets say I got 3 different type of view A, B, C.

Does it means I will have 3 Views, 3 Controller and 1 model?

or each view has its own model.

if each view has its own model.. how do I control each of the model?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

It depends. These 3 views could have the same controller, but could also have a different controller.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
but what about Model?

Must I have only 1 Model?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

That depends on your situations. For a book application you'll have a view to display all books, update a book, delete a book, view the details of a book and create a new book. So you'll have 5 views, 1 controller and 1 model. More info on MVC can be found in a design patterns book and on the internet (e.g. here). A design pattern usually comes in many, many flavors.

Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
after a smoke break...(haven't smoke for years)

I finally come out with a design.. I am going to have a panel with menuTab..

there will be 3 menu tab..

1) search tab .. able to view all, search, delete, enable edit tab go there.
2) create tab.. create new record
3) edit tab

1 model 1 view controller

....

--------------------------------------------------------------------------

just curious do i need a UI to allow the user to choose between server, standalone, UI with network?
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

keep in mind that only searching and booking should be available as per spec...

The user interface for this assignment must satisfy the following criteria:
• It must be composed exclusively with components from the Java Foundation Classes (Swing components).
• It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.
• It must present search results in a JTable.
• It must allow the user to book a selected record, updating the database file accordingly.


SCJP 5, SCWCD 5, SCBCD 5
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Jonathan Elkharrat wrote:keep in mind that only searching and booking should be available as per spec...


that means my UI will be

1) .. able to view all, search, delete,

that make my work much easier
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

And could you please stop quoting complete posts. This has no added value at all, certainly not when just 2 people are discussing in the same topic.

I don't like your UI design. Much too complicated.

The parameter used at startup ("alone", "server", <nothing>) specifies which application should run (according to your instructions). Of course you need to provide the user a little UI so he/she can enter the necessary configuration settings (ip, port, db location) depending on the mode the application is running in.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Ixus See,
Your post was moved to a new topic.
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:So you'll have 5 views, 1 controller and 1 model.


For this assignment you have only one view - your application that displays records. If you had a web interface (like Andrew Monkhouse implemented) then that would be a second view. If you implemented a mobile web application, then that would be a third view. That is the commonly understood use of the term View in the MVC pattern.


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Sean Keane wrote:That is the commonly understood use of the term View in the MVC pattern.

You can have multiple views based on the same model within the same technology (as described here). A view just renders the contents of a model. It's just a representation of the model data and thus when the model data changes, the view must update its presentation. Nothing's said about needing another technology.
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:
Sean Keane wrote:That is the commonly understood use of the term View in the MVC pattern.

You can have multiple views based on the same model within the same technology (as described here). A view just renders the contents of a model. It's just a representation of the model data and thus when the model data changes, the view must update its presentation. Nothing's said about needing another technology.


I think you've misunderstood the main point of my post. The main point was this assignment will not have five views as you mentioned. It will have one single view - i.e. the user interface that you develop.

My examples just happened to be different technologies. They could have all used the same technology. You seem to have mistakenly interpreted this as me saying each view must have a different technology. Which I never said
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Sean Keane wrote:My examples just happened to be different technologies. They could have all used the same technology. You seem to have mistakenly interpreted this as me saying each view must have a different technology. Which I never said

True, You didn't say it directly, but the examples you gave could insinuate different views can only happen with different technologies.

My example was also not suitable for the assignment, although you have 1 view with the table (to show all matching records). Would a dialog with the detailed information of a room (when you double click on a record in the table) be considered as a 2nd view?

I'll give another clear example from the Eclipse IDE. For a HTML file you can show your html file in source mode (just the source of your html), visual design mode (a wysiwyg editor), a split-screen source/design mode (the 2 previous modes combined) and a preview mode (how will your html page look in IE and/or Firefox). Each of these modes is a different view. All these views are based on the same model (your html page). When you make a change to one of these views, the model changes and the other views update their representation.
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

They were just examples. I could have given examples all with the same technology. Giving examples in different domains highlights the concept of a view. I think you just misinterpreted it

Whether a dialog showing the room information is considered a separate view is debatable. But I think we are getting away from the main point of the original poster. I was simply highlighting the concept of a View in the MVC pattern, as the original poster would have been mislead if they thought that this assignment had five views.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Sean Keane wrote:I was simply highlighting the concept of a View in the MVC pattern, as the original poster would have been mislead if they thought that this assignment had five views.

And what do you think I did with your example? Exactly the same: preventing the OP possibly could misinterprete your example (as I did too)
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

The difference is that stating the assignment has five views is incorrect. Whereas my examples were not incorrect - all of my examples were correct descriptions of different views when using the MVC design pattern.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Sean Keane wrote:The difference is that stating the assignment has five views is incorrect.

I never stated that
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Oh, you were talking about a book application! It is still misleading to describe things in terms of a book application, saying that deleting a record is a separate view in the MVC design pattern - it's not a separate view, it's just an operation you can carry out on the single view that you have.

Anyway, let's leave book applications to one side, for this assignment it is quite simple - you have one view
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
More question on MVC

My implementation of MVC

View implements observer- register all the button with an actionlistener and action listener call controller's method
Controller - contain methods which call the model.
Model extends Observable - contain all the logic methods calling DAO


Here are my questions:

1) Lets says I click exit button. can my action listener just open a JOption box with yes no and quit... (without calling controller) ?

2) Lets says I click View all Record ... should my implementation be

a) actionlistener -> controller -> model -> getAllRecords -> model.getRecords and model.setViewJTable without calling notifyObservers

b) actioner listener call model.getAllRecords directly and returns a set of records and set the JTable

c) other implemenation


3) Lets say a record is already book or deleted.. should the business of checking be in DAO or Model?

4) What is the best way to differentiate the different method that call notifyObservers in model? for example if someone updated a record and the record change should flow back to view.

How is this achieve and differentiated from a delete record command and JTable should remove it if it has it on the list.

5) Lets say client A update the record, therefore client B should show the change too if it exist on the JTable. How is this achieve? I don't understand how 2 clients on 2 different computer can be notify in such cases.

Sorry for the list of questions...
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

1/ I didn't show an extra JOption box, but I think it's fine showing it from the ActionListener

2/ In my application I have following flow: click the search button, controller-method is called, the controller makes a call to the model (the business service) and this service calls the find-method in Data class. The matching records are returned and are turned into a TableModel which is handed over to the JTable.

3/ My business service executes a number of business rule validations (e.g. room already booked). I decided to keep my Data class unaware of the type of data it's processing. So my Data class can easily be used to handle a similar database file containing customers or hotels or ...

4/ When a record is updated (with a customer id) the JTable is updated again with executing the search again with exactly the same filter. I didn't allow deleting records in my GUI, but I would implement it just like that again.

5/ I didn't implement a mechanism to push changes to different clients.
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:4/ When a record is updated (with a customer id) the JTable is updated again with executing the search again with exactly the same filter. I didn't allow deleting records in my GUI, but I would implement it just like that again.


Does this mean that when a client books a room in your application you submit a search request from the client? If so, what is the point of this? Could you not simply update the information locally in the application? I'm guessing you had a good reason why you couldn't just update the information locally - so wondering what it was?

When you book a room one of two things will happen (A) the room is booked successfully or (B) The room will not be booked because either something has gone wrong or the room is not available.

If the room was successfully booked then you could just update this information locally in the application?

If the room was not successfully booked then I guess a few things could have happened, (1) The room is already booked (2) The room has been deleted (3) Some error has occurred in the application.

So I guess you do a search after an attempt to book a room because of (1) and (2) i.e. the information currently being displayed to the user is out of date. So you redo their previous search so that the most up to date information is displayed.

Have I figured it out correctly ?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

I could simply update the client, but that would require some extra code. I just decided to renew the data in the table by executing the search again. That requires no extra code, because all the code needed to do this was already available. So that's less code and less code means less to maintain and less (chances for) bugs
 
Consider Paul's rocket mass heater.
 
subject: MVC