I have class A that depends on interfaces B & C. I use constructor injection to inject them into A:
class A {
let b: B
let c: C
init(b: B, c: C) {
self.b = b
self.c = c
}
}
This is mostly so I can inject mock objects for testing.
However, 80% of my use cases use the default implementations of B & C (let's call them, BImpl and CImpl). There's also some edge cases that make up 20% of my use cases.
Is it a good idea to create a "default" initializer where the constructor is parameterless and sensible defaults are created?
init() {
self.init(b: BImpl(), c: CImpl())
}
The initializer delegates to the previous (parameterized) initializer, so I don't lose my ability to inject my own (custom) dependencies if I wanted to. But this way, I can also choose to roll with the default values if I want to.
Does this have some unforeseen disadvantages?
B
andC
, so have to test that functionality as well.