title: Delegating events in Model-GUI-Mediator Pattern (possibly also MVP?)
body: <p>When an event fires that should act on the focused control (not the control that fired the event), how do you handle this in the Model-GUI-Mediator (MGM) pattern? I suspect this question also applies to the closely related <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter" rel="nofollow">MVP pattern</a> if you have more than one model.</p>
  
  <p>I titled this 'delegating events' because I think the mediator for the event-originating control has to delegate the event to the mediator for the selected control (or to its model) - but the question is, 'how?'</p>
  
  <p><strong>Background</strong></p>
  
  <p>The MGM pattern is a bit like <a href="http://martinfowler.com/eaaDev/PassiveScreen.html" rel="nofollow">MVP Passive View</a>, except that you have a separate 'mediator' for each control (rather than one presenter per form). A control's events are all handled by its mediator, and the mediator calls methods of its particular model in response. The mediator is an <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow">observer</a> of the model and updates its control when the model changes. It's particularly suitable in a RAD environment where your controls don't have data binding and you don't want to subclass them. It also has the advantage that there's very little boilerplate code to hook up events, unlike Passive View. Here are two more detailed descriptions:</p>
  
  <ul>
  <li><a href="http://geldenhuys.co.uk/articles/" rel="nofollow">Model-GUI-Mediator (Graeme Geldenhuys)</a></li>
  <li><a href="http://www.andypatterns.com/index.php/download_file/51/46/" rel="nofollow">Model-GUI-Mediator (Andy Bulka)</a></li>
  </ul>
  
  <p><strong>Problem Example</strong></p>
  
  <ol>
  <li><p>A form contains a number of controls that provide views on a number of model objects. Only one of these views can be selected at a time. (Imagine a multi-document editor if you want something concrete.)</p></li>
  <li><p>A user invokes a command from a menu. This calls the Execute method in the particular menu item's mediator object. (The menu item is a control, so it has a corresponding mediator.)</p></li>
  <li><p>The command is supposed to perform an action on the currently selected control.</p></li>
  <li><p>So the menu item's mediator needs to find either the mediator belonging to the selected control and delegate the action there, or it needs to find the model associated with the selected control's mediator and call that directly.</p></li>
  </ol>
  
  <p>But how does the menu item's mediator find the selected control's mediator?</p>
  
  <p>In MGM, mediator objects are supposed to be self-contained, so don't know about other mediator objects. The controls are not allowed to know anything about the models (to maintain clear separation of concerns). The only thing the controls know about their mediators are the event handlers.</p>
  
  <p><strong>Hacky workaround</strong></p>
  
  <p>The best I've come up with so far is to examine an event field in the selected control, which will be an object method pointer to that control's mediator. In Delphi I can cast this to a TMethod and hence extract the object pointer for the control's mediator. I can then cast this to the mediator's type and call the required method.</p>
  
  <p>But this seems to rely heavily on a language feature (TMethod) and also creates dependencies between the mediator classes.</p>
  
  <p>Perhaps I'm on the wrong track entirely…</p>
  
  <p>(P.S. Could someone with more rep than me please create a "model-gui-mediator" tag? Thanks.)</p>