I am building an application that should take a parameter specifying an implementation of a given data structure to use. Say, the super-type of the parameter is Super, and and there are many implementations as SubX, that I provide. There may be also SubY, not known at compile time, provided by the user of my application.
Within the application, the parameter will be passed all around, and will be used create instances of Sub_, invoke methods defined in Super, and used as a parameter in parameterized types.
If it were only for the parameterized type, I could make my classes parameterized too, and that would work. However, I need to create instances of such objects, and so I am wondering what would be the best way of configuring my application.
Should I pass the types plus factories as parameters? I find the factory pattern ugly and would like to avoid this approach. Besides, there would be too many parameters passed around (my application actually requires several of such parameters).
Should I create an object and stick this configuration in there? Could the object object be resolved at runtime, allowing the configuration not be known at compile time?
EDIT: Ideally, I would like having something like the following code. Could you provide a minimum working example based on this?
class Test[Type <: Super] {
def main {
val testVal: Type = new Type()
testVal.methodDefinedInSuper()
}
}
Use factories. If there are too many of them and you don’t want to create a factory of factories to unify them, then use a dependency injection framework to do so. Dependency injection frameworks even support XML configuration, which seems to be in line with what you want to do.
If you don’t know any dependency injection frameworks, first check whether whatever frameworks you are using don’t already provide it, and, failing that, check out Google Guice.
Example with a factory:
Example with Google GUICE:
Note that this style of injection on Google Guice is the least recommended of them all. I’ve used it because it’s also the most concise, to give a general view.
Note, however, that there’s no reason to use
Type <: Superin either case. That bound isn’t gaining you anything, so you could rewrite them like this:Anyway, in the first case you pass the factory as a constructor parameter, and in the second case you use one of Guices bindings to tell Guice what it should use to initialize
testVal.