Is there a way to implement this pattern in a generic way?
A dispatcher object and a bunch of worker objects all derive from the same interface.
Any method call into the dispatcher object needs to be dispatched (forwarded) to one of the worker objects (with all the arguments).
Each method would need to discover it's own name, find the corresponding method in the worker objects, discover the arguments, and then make the call. If possible, not using the variable argument mechanism.
Is there some way to do this? Reflection? Code generation?
It might not be particularly simple, but it's very solid - take a look at Castle.DynamicProxy: http://kozmic.pl/dynamic-proxy-tutorial/
One possible approach would be to have each method in the dispatcher object raise an event, and have all the worker objects subscribe to that event. (Think this is called a "multicast delegate" pattern).
I suppose this might not be quite as "generic" as you're looking for, but could be a simpler way to achieve mostly the same ends.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With