olze oli wrote:Hi, thanks for your answer. I am wondering how the chain of responsibility pattern should be realized.
Not having used OSGi, I'm wondering if there isn't any literature provided by
it on the best ways of handling such a situation.
If I get it right, your basic problem is this:
1. You have a download, which contains a payload of some indeterminate type.
2. You want to design a framework that handles each download in the applicable manner for its type
without writing a bunch of dispatch code.
Now, the normal way to do this, if the type were known in advance (ie, at compile time), would be to use
polymorphism, but you only know the type at runtime. One possible solution is to use reflection, but I'd advise against it unless you really don't see any other choice.
Another is, as Kayesh suggested, to implement a
Chain of Responsibility, which may be particularly apt if your payload types lend themselves to some sort of hierarchy. And yet another might be to look at the
Visitor pattern. You may even want some combination of the two.
I'm afraid it would take too long to provide a complete implementation here, but hopefully the links give you something to work with (I believe both have
Java examples).
I'd also suggest getting a good book on design patterns (of which the granddaddy is
this one), because it sounds like you're going to need one.
Winston