Reputation: 140112
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:
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.
Upvotes: 5
Views: 923
Reputation: 5951
Option #3 sounds like the Mediator pattern and is often the best approach when the update logic and communication between the objects is complex. It also has the added advantage of keeping the control logic and initialization centralized which tends to make tracing through and debugging these types of cases easier.
Upvotes: 4
Reputation: 7702
It sounds like you have the knowledge to answer your own question on this one but maybe just lack the confidence to dive in. Are you just exploring or are you stuck somewhere?
I would add that one extra thing you can do is place a component manager in between all of the objects to facilitate the communication. In the past when I've done something similar, the management object ended up just taking data from each component, merging the data together, and passing it along to the model as one big request. Each of my components had a delegate they would call and would pass data into and of course the implementation of that delegate was on my component manager.
I also let that manager object act as a proxy to the network I was waiting for the complexity to get higher before I adhered to SRP and made that responsibility its own class.
Basically everything you said sounds good. I'd recommend to just dive in and explore.
Upvotes: 0