Question about MVC in Swing application
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
"What you don't know, can't help you"
SCJP (81%), SCWCD (81%), SCJD (354/400), SCBCD (85%)
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
I don't think it's necessary that the controller have no reference or dependency on the view. What do you think?
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
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
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 ]
"What you don't know, can't help you"
SCJP (81%), SCWCD (81%), SCJD (354/400), SCBCD (85%)
Stan James
(instanceof Sidekick)
Posts: 8791
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
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
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
"What you don't know, can't help you"
SCJP (81%), SCWCD (81%), SCJD (354/400), SCBCD (85%)
| He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad? Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |










