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

MVC again... please help

 
Ling Chung
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been delving into the MVC and seem to get the grasp but i need a little push... :roll:
In my Client i have the interface that talks to the model, the client itself doesn't do anythinh with it, but the fact that it know it is against MVC i think...
in my Client i have inner classes (Controllers) that do either a search or a book or handle any event. So controller performs a task e.g. findbycriteria. It sets the results in the TableModel, but actually it should have been the model that signals the view... I'm kind of confused here So here is an example of my code :

What do you guys think, it has to be differently without using the inner classes i think, could someone do a suggestion or help me out in the right direction please
Thank a lot!!!
 
Stephen Galbraith
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think you are almost there, but you don't have enough separtion between the layers
In MVC you have the following relationships...

You can think of your inner class as the controller (i'd make this a separate class and not an inner class). You already have a model (contractor table model).
So your view should ask the controller to do the book.
The controller should book the actual record. Now you want to
do extra tasks (update the model). This update task also sits in the
controller, when it gets the new data back it'll set the data in the registered model. Now the model can examine the data and tell any listeners registered with it that they need to refresh.
In psuedo code...

Obviously this won't work "as is" - you need to register the view(s) with the model, the controller with the view etc, but hopefully this helps.
Steve
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven, in your example code, you have the view having a reference to the Controller, this is not good, you are now coupling the Controller to the View. The view should only know how to display itself. But by having the Controller in there, you now have the view needing to know the Controller, what methods it has, and have some almost business logic in your view.
Mark
 
Ling Chung
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen thanks for the reply but in your example isn't it the controller who signals the view to update? Shouldn't it be the model who signals the view?
Could anyone jump in here with an example?
Thanks a lot!!
 
Stephen Galbraith
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
yeah - the view I've given here does more than just view - it also accepts user input from a button (book button which calls book on the controller), hence the need to delegate that functionality out to the controller (I guess you could call View a view controller ). In the example given I have closely coupled the view and controller, so the view "is" the controller in effect, and delegates the actual booking to another controller. . Ooops. So thanks for pointing that out.
Ling,
It is the model that updates the view - via the fireDataChanged method. The controller updates the model, even in the "confused" code above.
[ April 05, 2004: Message edited by: Stephen Galbraith ]
[ April 05, 2004: Message edited by: Stephen Galbraith ]
[ April 05, 2004: Message edited by: Stephen Galbraith ]
 
Ling Chung
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the view doesn't have the controller, but i give the view to the controller, do i give e.g. the entire frame, because for instance i need to check customerid and set statusmessages in my view.
for instance
status : Now booking
booking finished
etc.
How do i do this, do i set these messages in the controller which has the view at that moment?
Thanks!!
 
Ling Chung
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i would really like to complete my design by implementing the MVC in the correct way. So can somebody please help me?
The question is this :
If i have the view, the controller outside the view, so no inner classes and i have the model.
i pass the view to the controller ( do i pass the entire Client GUI, so i can set certain messages based on the result of the search or book action?)
Can somebody help me please i appreciate it very much or need it very much
Thanks a lot!!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well there are a few choses. You can create get and set methods to access the private JComponents that are in your GUI, then the Controller can call those methods and get access to the JButton as an example.
You could also have the JComponents that are in the GUI be public, which is how my GUI looked. With one exception, I had a JPanel that had the JTable in it, so I would get the JPanel, and call a method that I created to return the JTable, and also get or set the TableModel.
I had one class that was the GUI, this is the class that my controller had a reference too.
Good Luck
Mark
 
Stephen Galbraith
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the "Main GUI" panel implement an interface from which I could get the information the controller needs. That way I could swap in a different view (if needed) and not change the controllers
Steve
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephen Galbraith:
I have the "Main GUI" panel implement an interface from which I could get the information the controller needs. That way I could swap in a different view (if needed) and not change the controllers
Steve

Which is always a good idea. Programming to an Interface always gives you much more flexibility.
Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic