2

I've just finished working my way through the Head First Design Patterns book and I noticed that the chain of responsibility pattern is "Commonly used in windows systems to handle events like mouse clicks and keyboard events."

To what extent is this pattern used for these events and in what way?

I understand that the chain of responsibility pattern can act like a sort of filter, but what exactly would be filtered? Or in other words what different ways might these events be handled by the chained objects?

The first thing that comes to mind is some sort of mapping system but that doesn't seem to likely, and would surely be pretty inefficient?

Perhaps something to do with combinations of keys (Ctrl + Alt + Del etc.)?

Maybe I'm missing something obvious or have misunderstood something, but it's not instantly apparent to me why this pattern would be useful in this situation.

Josh Hardman
  • 721
  • 6
  • 17
  • Have you ever programmed web applications? Think of how events are bubbling in the DOM and how that might be useful in some scenarios. For instance, clicking a table cell may end up being handled at the row level to update the row selection model. – plalx Aug 27 '18 at 13:31
  • I haven't, but perhaps you're describing something akin to the demo in [this article](http://www.javascripter.net/faq/eventbubbling.htm)? – Josh Hardman Aug 28 '18 at 01:31
  • Well, the point being if you imagine a UI constructed of hierarchical components. You may have a `TableRow` made up of many `TableCell`. You may then want to handle the `click` event originating from clicking a `TableCell` at various levels of the hierarchy. Having component events such as `click` to bubble up in the hierarchy forms a kind of chain of responsibility, where every event handler can control how the event further propagates and execute specific logic. In the above comment, that logic could be that the event is handled at the row level and logically selects the entire row. – plalx Aug 28 '18 at 13:22

0 Answers0