What are some best practices to orchestrate the interaction between complex components that are in your View?
I'm not talking about simple widgets like a combo box or a grid control but components that are made up of multiple widgets and that may deserve being unit tested on their own.
Would you:
- Define abstract interfaces for each component,
let the Controllerhook them up via dependency injection to let them directly talk to one another via method calls? The components are therefore aware of other components' interfaces. - Define events that each component can fire and
let the Controllerdirectly hook them up via event listeners on one another? The components therefore have event handlers attached to other components' event sinks. - Define abstract interfaces for each component, define events that they can fire and
let the Controllerlisten on all events and perform method calls on the interfaces? The components therefore are completely agnostic towards other components. - A classic application of the Observer pattern?
- Anything else?
Update: I've stroke out "let the Controller ..." from #1-3 because it's not necessarily the Controller that has to do the routing/orchestration in those cases. It could be the View itself.
I've adopted method #3 in a recent project and I was happy with the decoupling and individual testability of the components. However, I have a feeling that I could streamline the wiring up of the components. In my case, the main View object has to add multiple event listeners on each component and then call methods on appropriate components, after sometimes doing some local processing (like talking with the Model). The code for adding the event handlers looks a bit messy and I'm particularly looking for a clean way of doing that.