• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Question about MVC in Swing application

 
Ranch Hand
Posts: 111
PHP Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I hope this is the right forum for my question....
I am orking on a Swing application which I have designed around the MVC architecture. The overall design is:

AbstractView (observer) <>------> BusinessController <>-------> DataModel (observable)
^
|
SwingView

The AbstractView class holds a reference to the Controller class, which in turn holds the model. The SwingView class extends AbstractView.
Furthermore, the AbatractView class registers itself as an observer of the Model class. So when the model changes, the View can update itself.
This works fine. But now, in the Controller class I carry out business operations, which, of courese, can throw exceptions. I want to send these exceptions to the view so the view vcan show them to the client.
But how to send onformation from the Controller to the View ? The view is an observer of the Model, but not of the Controller. So I cannot use the notifyObservers/update methods of Observable/Observer !
Of course I can pass in a (callback ? reference to the View into the Controller. But is this good practice ? What if I have more than one view ? The Observer pattern can easlly handle more Observers. Should I register the view to the Controller to, so the Controller can send (update) messages to the registered View(s).
I hope you can give me some hints !!!
TIA, Klaas
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the stimulus for the methods in teh controller that might run into trouble? If it's a method call from the view, you can just throw exceptions, right?

I don't think it's necessary that the controller have no reference or dependency on the view. What do you think?
 
Klaas van Gelder
Ranch Hand
Posts: 111
PHP Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanx for your reaction !
Indeed the controller-businessmethods are called from the view, so i can let them throw exceptions and handle them in the view.
But I am just thinking what is good design. Maybe I want to handle the exceptions in the controller and implement a method in the controller like:
private void showErrorMessageToView(String message)
which is then called from the appropriate catch clauses.
But then the Controller clearly needs a reference to the view ! You say that this is not a problem, and probably you are right...
But the controller definitely does not want to know what KIND of view is used, a Swing view, Web view, text based view or whatever.
Therefore I think of an interface that the view implements and that defines some "callback" methods. Each possible view must implement that interface, which is then passed in to the controller.
But then I can use only one view at a time. In fact I never want to use more than one view, but from a OO-design view maybe it should be possible that more views are used at the same time... In that case, the controller should hold references to each view, which means registering of each view to the controller.
I think I will choose for some ViewReference interface, which will be passed in to the controller. Then of course i am restricted to one view at a time.
Maybe I am thinking much too far... but learning to do good OO-desing is one of my targets at the moment :-)
Regards, Klaas
[ August 07, 2005: Message edited by: Klaas van Gelder ]
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have a good handle on the options. You can have an interface that is "owned" by the controller component and implemented by the view components. The register and notify technique you described lets any number and type of views hook up to the same controller at the same time.

I wonder if the method call for error notification should be in this pub-sub scheme ... an error generated by one view might result in a callback to all views.
 
Klaas van Gelder
Ranch Hand
Posts: 111
PHP Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx, and indeed a controller method to send messages to the View(s) will affect each View, if there are more than one... And this is probably not desired.
So I think I go for the business methods throwing some BusinessException, and handle them in the View. All business methods are called from a javax.swing.Action descendent, so it is not difficult to create some custom XxxxxAbstractAction class which can take care of rendering the error (or user info) messages in a generic way.
The view still needs to be notified if some Model data is changed. But I use several Panels in my view, which all affect some part of the Model data. I am still thinking of the right way to update/repaint only that part of the View which is affected my a given part of the Model...

Thanx & regards,
Klaas
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic