Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Layered MVC and Chain of Responsibility via Events

 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've noticed that user interface often comes to "layered MVC" (pardon me, I just came up with this new term). Let's take Swing table. It has a model (AbstractTableModel) and a view (JTable). When I use it, I do not put my business data in table model. For me both JTable and corresponding table model is only a part of my own View, which is in example a dialog. So this looks like MVC inside MVC.

Now I have a controller which contains FrameWindow and view cache object. Views are not created until requested by the user. View cache contains all created views. Like this:



I have a view which may require displaying a dialog box upon a button click. I consider this dialog a view. But there is a problem. Dialog needs to be displayed as child of FrameWnd and also positioned in center of it:



Is it nice to pass FrameWnd all the way down hierarchy? It is a controller job to display views, isn't it?

In MVC view notifies controller about user actions by firing events. I am thinking of implementing something like Chain of Responsibility pattern using events: bottom level component fires event, its parent anticipates event and if it can not solve the problem it fires his own event including cause/original event and so on until somebody takes action. That would be technicaly alike throwing exception with specified cause (new exception references previous one). Since event object contains event source, the handler of event (in my case controller) will get all objects along hierarchy path.

What do you think about this solution? Maybe there is more elegant way? While I wrote this, I thought maybe I could somehow remove this "view contains other view" thing. Still what do you think of chain of responsibility implementation using events?

Best regards
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't say I followed all of that, but Sun docs (or maybe the only Swing book I ever bought and can't find) talked about Swing using MVC internally. It sounded pretty compelling at the time. As systems are built up of subsystems it shouldn't be surprising to find recursive or repeating architectures and patterns.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vladas,

I wonder if these links about Hierarchical MVC are related to your question.

http://www.javaworld.com/javaworld/jw-07-2000/jw-0721-hmvc_p.html

http://scope.sourceforge.net/index.html
http://scope.sourceforge.net/presentation/hmvc.html

Thanks to Ali Pope for suggesting I look into HMVC.

Regards, Marlene
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vladas your idea is quite nice and is called HMVC as already pointed by Marlene. One little correction:

It has a model (AbstractTableModel) and a view (JTable). When I use it, I do not put my business data in table model. For me both JTable and corresponding table model is only a part of my own View, which is in example a dialog.


In this case you already have the model and the view, not just the view .

./pope

ps: follow the links Marlene posted. They will give you a much clear idea.
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Truly great pages. Thanks!
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vladas. I was wondering if you had found our comments buried under all those HF posts. Marlene
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic