InterviewSolution
| 1. |
Which dependency injection pattern is best and why? |
|
Answer» Constructor Injection is better than other patterns from all other patterns It is advisable to utilize constructor injection for every mandatory collaborator and SETTER injection for every single other property. Once more, constructor injection guarantees every required property have been fulfilled, and it is essentially impractical to instantiate an object in an invalid state (not having PASSED its collaborators). When utilizing constructor injection you don't need to utilize a dedicated MECHANISM to guarantee required properties are set (other than typical Java mechanism). In case if we are using field injection which is non-final, they are prone to circular dependencies. Though it exposes coupling if there are more than 3 objects as part of the constructor injection. It is ALSO easy to test if we use constructor injection. |
|