I have
trait Builder[T, K] {
def build(brick: T) : K
For that trait, i have multiple implementations...
class StringBuilder extends Builder[Foo, String] { ... }
class HouseBuilder extends Builder[Baa, House] { ... }
class BaaBuilder extends Builder[Baz, Int] { ... }
Depending on a given Type, i would like to choose from one implementation. Something like this (Pseudo-code):
class BuildingComponent @Inject()(builder: Set[Builder]){
def doIt(item: Any) = {
item match {
case _: Foo => builder.filter(Foo).build(item)
case _: Baa => builder.filter(Baa).build(item)
case _: Baz => builder.filter(Baz).build(item)
}
}
So 2 point:
how could i inject all implementations of the trait "Builder"?? I found a bunch of questions that go in the same direction (using multibinder, TypeLiteral etc. but none of them facing the problem of injecting all implementations. Its just about "how to inject a specific implementation") I know how to bind multiple instances using multibinder; but not if it is a generic class...
In the end i would like to use kind of facade-pattern. having one "builder" to inject, that gets all implementations injected and knows what builder is needed (see match-case-fracment above). But instead of using match-case, i had an eye on the MapBinder. Something like binding the Builder-implementations to a Map, that uses the class as a key.
e.g. (Pseudo-code)
class BuildingComponent @Inject()(builder: Map[Class,Builder]){
def doIt(item: Any) = {
builder.get(item.class).build(item)
}
}