April 2018
Intermediate to advanced
246 pages
6h 11m
English
Passing dependencies with a constructor is more clear in terms of describing what is required to create an object. You may write multiple versions of constructors, each taking a different combination of dependency objects, if that is allowed.
Alongside initializing fields with the constructor, you can hide them by not providing a setter method. The advantage of this arrangement is that you can make sure the dependencies being set through the constructor will be available for the lifespan of an object. This is important, because if you do not want a particular dependency to be changed with the birth of an object, then initializing it with the constructor and not providing a setter will make it immutable. A constructor-based ...