Can a delegate setter be in an interface?
We have many objects with this design: an interface and several implementations, as well as the use of several objects for composition. Example: Foo implements IFoo and has a Bar object that implements IBar Foo also has a setBar (IBar bar) method for dependency injection.
My question is, the setter cannot be in the interface? (For testing, bullying ... I'm stuck!)
a source to share
To set a setter in an interface just for taunts and testing is not good. Thus, you allow users of this interface to have arbitrary collection components, even though the properties of this object probably should not be modifiable after construction. The interface shouldn't reveal how to construct an object.
a source to share
Alternatively, inject Bar into Foo using setter Injection. In a context where Foo cannot exist without Bar, it would be more appropriate to inject Bar with the constructor.
Further reading: Types of dependency injection
a source to share