• 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

UI chain of responsibility vs concrete events

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

I am thinking about usual UI design problem. How component or controller notifies it's owner about events. I see 2 solutions:

1. Use chain of responsibility and pass event until someone handles it. Basicaly we get single listener (or method) for all events. So you just add single listener for all events.

2. Use multiple listeners each for it's own event. This way only interested components are notified.

In this case I tend towards "2.multiple listeners". In example, I have child component contained by parent. In the parent I can expose any events child may throw. I can pick up which ones to expose and which ones to hide. This functionality is also possible with Chain of Responsibility, but CoR does not provide clear contract in form of addListenerForEventA, addListenerForEventB. It's hard to tell what events are handled where, using CoR.

Any suggestions to support or to criticize my thinking? I am grateful for both!

With best regards,
Vladas
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd advice to only use Chain of Responsibility if you need actual event data (such as the coordinate of a mouse click) to decide which component should handle it. I wouldn't use it if I already could decide at object composition time what component should handle events from which source.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've only used chain of responsibility in nested components. If a sub-panel can't handle an event, pass it to the containing panel. If it can't handle it, pass it to the tab page. If that can't handle it, pass it to the tab set, and so on. And I guess I've always done it in frameworks - abstract classes where you have the option to override a method anywhere in the hierarchy or let the event bubble up to default behavior at the top level.
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found that CoR and Events have a subtle but important difference for this problem. CoR is more appropriate when component requires some service but doesn't know who will provide it. Events are more appropriate, when component doesn't really care if any service is provided at all.

Just my thoughts
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic