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.
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.
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 ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
subject: MVC & Swing - How model classes work together ?