Dependency Injection
Dependency Injection (often called Inversion of Control) is a
programming pattern that aims to keep the various components of a system
independent, and couples them loosely at runtime. That is, instead of
having code like:
public class Foo
{
public void doer() {
BarInterface b = new BarImplementation();
b.doit();
. . .
Which creates a direct dependency between the class Foo
and the class
BarImplementation
, we allow the class to be given to us (injected), as in:
public class Foo
{
BarInterface b;
public void setBar (BarInterface b) {
this.b = b;
}
public void doer() {
b.doit();
. . .
This pattern is also called the Hollywood Principle since it describes the
"Don't call me, I'll call you" attitude. Yeah, geeks have to inject their own
humor whereever they can.
In this presentation, I give a high-level overview of this programming pattern
and how some IoC frameworks implement it. It is not a complete description of
the concept, nor does it accurately describe these popular frameworks, like
the Spring Framework or Google's Guice.
BTW: A really good article on this subject is from Martin Fowler.
Tell others about this article: