The problem domain: An issue exists where in a modular application you want to be able to share events from different modules without creating a dependency between the two. Deeply nested objects make this even more apparent when you realize you can’t use bubbling since they don’t reside within the same branch line down to the root trunk.

design-pattern-example

The solution: Is actually far more simple and elegant than you might at first realize. By using the Singleton pattern with a Bridge, you have the ability to create an event proxy distribution manager. This allows any event that an object would otherwise dispatch, instead is dispatched through this manager. As an example of something I recently faced, sub-controls used in an ItemRenderer, events coming this control needed to update data in a permanent part of the application (or could just as easily be another module).

An excerpt from the ItemRenderer:

button.addEventListener(MouseEvent.CLICK, onReadMoreClicked);

protected function onReadMoreClicked(event:MouseEvent):void {
EventProxyManager.getInstance().dispatchEvent(event.clone());
}

Where the EventProxyManager is:

package {
import flash.events.EventDispatcher;
public class EventProxyManager extends EventDispatcher {
private static var instance:EventProxyManager;

public function EventProxyManager() {
if (instance) throw new Error(‘Singleton pattern: use getInstance()’);
}

public static function getInstance():EventProxyManager {
if (!instance) instance =new EventProxyManager();
return instance;
}
}
}

Literally that’s it. Now any deeply nested object can share events across non-intersecting branches, without using a parent proxy system or forcing a dependency on the distribution node.