'How to use Implicit optional for a delegate
I want to use implicits for Dependency injection, in my code I want to provide a default implementation but I want to give the person who might use my Class to inject its own implementation but I can't figure out best way to do it.
General is something likes this
class ClassA {
val something : ClassB = new ClassB()
something.eventAdapter(new MyDefaultImplementation())
}
class MyDefaultImplementation(implicit specificImplementation: EventAdapter[EventA, EventB] = null) extends EventAdapter[EventA, EventB] {
override def doSomething(event: EventA) : EventB = {
if(specificImplementation != null) {
specificImplementation.doSomething(event)
} else {
event match {
case _ => new EventB()
}
}
}
}
and if some other developer want to provide another implementation.
object OtherDeveloperImplementation {
implicit val otherImplementation = OtherDeveloperImplementation()
}
class OtherDeveloperImplementation extends extends EventAdapter[EventA, EventB] {
override def doSomething(event: EventA) : EventB = {
convert(event)
}
}
My dilemma is, I don't know what the other developer will call his class, so I can't make an import in 'ClassA' something like the following.
class ClassA {
import OtherDeveloperImplementation._
val something : ClassB = new ClassB()
something.eventAdapter(new MyDefaultImplementation())
}
because I don't know how the developer name his class, how should I deal with this?
Thx for answers
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
