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!)

0


a source to share


3 answers


If you are using polymorphism i.e. call the installer by interface type, then obviously you need it in the interface.



0


a source


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.



+1


a source


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

0


a source







All Articles