J
Jordan S.
Does the following accurately summarize the notion of "dependency
injection?"
Class A needs to cause "Some Activity" to happen (in addition to other
things that Class A does).
Class A may or may not have a default implementation of Some Activity.
Class B is able to do Some Activity.
At runtime, a reference to an instance of Class B is passed to Class A
(perhaps via Setter, or to the constructor of Class A).
Class A causes Some Activity to happen by calling a method of its Class B
reference.
In the future (or even immediately), we need for an alternative
implementation of Some Activity, so we create Class C.
We can then pass a reference [to a Class C instance] to Class A, which then
initiates Some Activity on the Class C instance.
In the above account, Class B and Class C both implement an interface or
derive from a common abstract base class -- and Class A knows only about
that interface, and initiates Some Activity as a method of that interface or
base class.
The most important benefit of the above arrangement (dependency injection)
is that we can extend the application specifically to perform alternative
implementations of Some Activity by adding a new class and updating the
application configuration (so that Class A can be passed a reference to the
new class), without modifying Class A in any way. In other words, Class A is
open for extension, but closed for modification, and dependency injection is
one way to achieve that objective.
Your comments are appreciated, as I'm wanting some verification or
clarification of my understanding of dependency injection.
Thanks.
injection?"
Class A needs to cause "Some Activity" to happen (in addition to other
things that Class A does).
Class A may or may not have a default implementation of Some Activity.
Class B is able to do Some Activity.
At runtime, a reference to an instance of Class B is passed to Class A
(perhaps via Setter, or to the constructor of Class A).
Class A causes Some Activity to happen by calling a method of its Class B
reference.
In the future (or even immediately), we need for an alternative
implementation of Some Activity, so we create Class C.
We can then pass a reference [to a Class C instance] to Class A, which then
initiates Some Activity on the Class C instance.
In the above account, Class B and Class C both implement an interface or
derive from a common abstract base class -- and Class A knows only about
that interface, and initiates Some Activity as a method of that interface or
base class.
The most important benefit of the above arrangement (dependency injection)
is that we can extend the application specifically to perform alternative
implementations of Some Activity by adding a new class and updating the
application configuration (so that Class A can be passed a reference to the
new class), without modifying Class A in any way. In other words, Class A is
open for extension, but closed for modification, and dependency injection is
one way to achieve that objective.
Your comments are appreciated, as I'm wanting some verification or
clarification of my understanding of dependency injection.
Thanks.