45

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?

6
  • 20
    .. because "Dependency injection" is not a pattern! Commented Feb 20, 2012 at 12:51
  • 1
    @DipanMehta stackoverflow.com/questions/823387/… Commented Feb 20, 2012 at 12:53
  • 16
    Everything that gets repeated forms a pattern of repetition. All design elements (that aren't unique, crazy ideas) are "patterns".
    – S.Lott
    Commented Feb 20, 2012 at 13:31
  • 4
    These long responses are probably misleading, as they kind of validate the question. While, as mentioned, dependency injection is not a design pattern. It is a "mechanism" for object instantiation, typically handled by the framework. Commented Oct 23, 2012 at 17:33
  • 4
    Dependency injection is a pattern. It opposes the Service Locator pattern. Read Fowler who coined the term. I have absolutely no idea how so many people upvoted such nonsense.
    – James
    Commented Feb 27, 2015 at 20:23

4 Answers 4

111

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 main(). If you worked on a large project, you might have a (usually quite elaborate) makefile to create a module-based program. But "unit-testing"? Starting a process, building the necessary memory context, executing it, and tearing it down, on a per method basis? That was very radical.

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!").

3
  • 18
    I regret that I only have but one up vote to give for such an insightful answer.
    – user8
    Commented Feb 21, 2012 at 0:54
  • @Larry OBrien: scanning for convention based registrations vastly simplifies configuration code and practically eliminates xml configuration in ioc containers. Commented Feb 21, 2012 at 3:36
  • 5
    I would like to add that dependency injection at its core does not rely on configuration files at all. You can do it all by hand, which makes it very easy to use and is still a very flexible approach. Commented Oct 23, 2012 at 14:34
34

They called it Strategy.

Their Strategy seems to have all the features of dependency injection without the complex-sounding name.

18
  • 17
    -1. Sorry! Strategy pattern has nothing to do with Dependency Injection. Commented Feb 20, 2012 at 12:50
  • 24
  • 15
    @Dipan: before going to downvote this, you better think about the answer five minutes.
    – Doc Brown
    Commented Feb 20, 2012 at 13:20
  • 7
    it's true that Dependency Injection can be considered very similar to the Strategy Pattern, but when people say dependency injection they usually mean Inversion of Control, which I think is a lot more distinct from Strategy (esp. IoC container).
    – MattDavey
    Commented Feb 20, 2012 at 13:38
  • 9
    DI is more of a creational pattern. It creates and injects strategies. Saying it's a strategy is just half the truth. DI is more a microkernel pattern! I can't believe people upvote this. Strategy is more like a trait of good DI, not a necessity.
    – Falcon
    Commented Feb 20, 2012 at 17:52
0

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.

1
  • 2
    This doesn't answer the question. Please read the original question instead of replying to other answers :)
    – Andres F.
    Commented Oct 23, 2012 at 14:32
-5

the answer Strategy is 100% correct. I up voted it but can comment.

"Strategy lets the algorithm vary independently from clients that use it.[1] Strategy is one of the patterns included in the influential book Design Patterns by Gamma et al. that popularized the concept of using patterns to describe software design."

A design pattern is not dependent on its use. Dependency Injection is implemented by using the Strategy pattern. If we named each pattern based on the use case we would have to rename alot of patterns.

The repository pattern is not a new pattern, it is the Template Pattern.

"In the template method of this design pattern, one or more algorithm steps can be overridden by subclasses to allow differing behaviors while ensuring that the overarching algorithm is still followed."

Often the patterns are multiple patterns combined and named such as the MVC pattern.

The GOF didn't have interfaces the used Pure Abstract classes, and also took advantage of C++'s ability to inherit from more than one class.

1
  • 1
    The purpose of a pattern absolutely is important in distinguishing. Among GoF patterns, Adapter and Proxy are good examples - they have the same form, but different purpose. I'd also disagree with your assertion that DI is implemented using Strategy; Strategy is more specific in its purpose and the way the configured object is used, so it's more reasonable to say that Strategy is implemented with DI.
    – Jules
    Commented Mar 3, 2016 at 19:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.