• 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 & Swing - How model classes work together ?

 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I'd like to hear your opinions about how to make model classes work together in a Swing App.

From my previous experiences with web apps, I know the controller commonly puts all info needed by views as a kind of attribute in session, request or application scopes.

However, I don't know what approach is used with Swing apps.

Reading the Java Tutorial from sun I could understand that in Swing the MVC is more a Model Driven approach than a really MVC pattern as we are used to hear in the web app world.

I also know that in a Swing app, my forms (JFrame, JDialogs ...) may be considered as view, the event listeners as controllers and my custom classes as models, but I'm not confortable about how to stick everything together, do you know what I mean ?

Well, just to simplify my doubt a little bit, let me use the following scenario :

I know it's not a useful real world app but it's just for understanding what I have in mind.

Scenario :
----------
Imagine I have a Swing app where the main window ("Product List Screen") contains a JTable with all products with its respective code, descriptions, unit price, stock quantity and so on ...

On this same window, there's a button ("New Product") which opens a new window ("New Product Screen") for creating the product.

So far, I could identify :

View : The two windows I mention above ("Product List" and "New Product")

Controller : Action Listener used to listen the button click.

Just continuing ...

After open the "New Product" screen, the user is expected to input all product info and them submit the info for insertion.

Here starts my doubt : How to inform the view ("The Product List Screen") of a new product inclusion and ask it to re-read all information from database ?

Even better, how to pass this new product info to the "Previous Product List Screen" as a bean avoiding the need of a read from database ?

If I was using a WEB app, I simply would put such bean as a request attribute and then redirect to the view. But .... What if I'm using a Swing app ? Is there some kind of shared area to put such beans and other model classes ? I don't think so.

I'm really interested about your comments and tips.

TIA,
Edisandro.
[ September 20, 2006: Message edited by: Edisandro Bessa ]
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edisandro Bessa:
How to inform the view ("The Product List Screen") of a new product inclusion and ask it to re-read all information from database?



Usually the Observer Pattern is used in MVC to accomplish this. The model publishes certain types of events like "new product". Anyone wishing to receive events from the model simply "subscribes" to certain events by implementing an Observer interface and registering that with the model. Whenever something in the model changes, the model would notify the registered Observers of the event. So:
  • The "New Product" View/Controller submits the new product to the model.
  • The model obtains the list of observers that registered for "new product" event an notifies each one in turn.
  • The "Product List" View/Controller is one of those registered observers. In response to the "new product" notification it adds the new product as specified to its list.


  • Have a look at EAA-dev: Observer Synchronization and EAA-dev: GUI Architectures.
     
    Edisandro Bessa
    Ranch Hand
    Posts: 584
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hum I see.

    Thank you very much Peer for your prompt reply.

    So please, let me ask something more :

    After your explanation I quite understood how to make communication occur between Model and View but my doubt now is :

    Who will create this model ? Product List Screen ? New Product Screen or during the application load ?

    I'm asking this because if I create the model in the New Product Screen, at this time no observer is registered yet. So did you get my point ?

    Thanks a lot.
    Edisandro.
     
    Peer Reynders
    Bartender
    Posts: 2968
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Edisandro Bessa:
    Who will create this model ? Product List Screen ? New Product Screen or during the application load?



    For "classic MVC" the model is established during application load.

    However "Swing MVC" seems to focus on the model of the controls - not the domain model!

    And in the web world "MVC" seems to have been highjacked for what should be a simple matter of "separation of concerns".

    Spend some time reading and understanding Fowler's EAA-dev: GUI Architectures article. MVC has its warts(especially in Swing). There may be better ways towards achieving a separated presentation. Then again your concerns may be addressed with the introduction of a Presentation Model (Application Model).

    See also Fake MVC in Swing - Is real MVC possible?
     
    Edisandro Bessa
    Ranch Hand
    Posts: 584
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay Peer.

    Thanks a lot again for your prompt reply.

    Your posts were very helpful for me.
     
    Peer Reynders
    Bartender
    Posts: 2968
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Now THIS does look interesting
    Presenter First: Organizing Complex GUI Applications for Test-Driven Development (PDF)
    from the Presenter First Page.

    This approach starts out with a variation of the Model-View-Presenter pattern (Martin Fowler has split this pattern into the Supervising Presenter and Passive View pattern) and takes Michael Feathers' The Humble Dialog Box(PDF) approach and evolves it for more complex GUIs.
     
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In GUI Architectures Fowler says:

    In this essay I want to explore a number of interesting architectures and describe my interpretation of their most interesting features. ... To some extent you can see this essay as a kind of intellectual history that traces ideas in UI design through multiple architectures over the years.



    It's a good read that may lead you to your own mix of features and ideas.
    [ October 12, 2006: Message edited by: Stan James ]
    reply
      Bookmark Topic Watch Topic
    • New Topic