Why was the dependency injection pattern not incluided in the gang of four? Did GOF pre-date widespread automated testing? Is dependency injection now considered a core pattern?
I was Editor of Software Development magazine when the Gang of Four book came out and I can say with total confidence that unit-testing was not a widespread practice in 1994, when Design Patterns was originally published.
In 1994, C++ was the most commonly used object-oriented language, and most people programming it were coming from a C background. One of the "thinking in objects" things that people simply didn't have is the idea of hundreds or thousands of entry points into your program. You thought about the
Java made multiple-entry-point programming more obvious. By the time of the original Dot-Com boom, unit-testing was a well-known technique, but it was really JUnit (circa 2001?) that caused it to catch fire and become a universal practice.
Although Strategy and the general concept of programming to an interface were part of GoF and the mid-90s zeitgeist, the idea of injection came quite late to the party (circa '03-'05?). Honestly, my gray hairs are still quite dubious about that aspect of DI ("Get off my lawn, you darn configuration files!").
They called it Strategy.
Their Strategy seems to have all the features of dependency injection without the complex-sounding name.
|show 13 more comments|
According to this,
The document cited above shows a good example that a Copy module while itself is abstract one, depends on lower level details of source and destination type breaking this principle.
When design evolves, one must check whether this principle is violated. The simple way to test the virtual class against DIP is to create, inside a unit testing framework a pluggable class. Ideally, i would plug such a class - that the base class shouldn't have heard-of or designed for. If the primary functions works as expected - it means that base class is reasonably abstract and doesn't quite violate DIP!
This process of injecting such a class to test is Dependency Injection. As wikipedia says
There is no one way to apply dependency injection; it can be done in many was and mostly it would use some other pattern itself.
According to me it is a well understood method for a particular task; but not really so unique that it be called a pattern of it's own.
Here is another great example which explains what is Dependency Inversion Principle and shows how a unit test framework is used to uncover this!
|show 3 more comments|
I think Dependency Injection is more relevant when separating implementation in tiers. Another area where we think about dependency injection is unit testing. And your pre-date suggestion seems to be correct. If the gang were to collect and segregate patterns in 2012, definitely dependency injection will be there.
Strategy could come up in discussions but Strategy does not talk about dependency injection. But when using strategy pattern in a single project or dll(all classes and interfaces remain in one project) it appears that we are doing dependency injection. In fact we are not.
Now, if the classes and interfaces mentioned in strategy pattern are separated in different projects or tiers then WE will have to use dependency injection techniques. We could use unity configuration files(no runtime change possible though). But Strategy pattern does not say how to inject a dependency.
If there is a pattern that resembles closely to Dependency injection then it is Abstract Factory Method pattern. This pattern could be used inside a strategy pattern to inject dependency.