0

I keep reading that object x has dependencies. Some people say it's bad, others okay in certain situations, but I don't understand what it means in the first place. I saw this:

What is dependency injection?

But didn't understand the dependency concept to begin with. That tag actually gave a good definition, but was hoping for an example.

Community
  • 1
  • 1
johnny
  • 19,272
  • 52
  • 157
  • 259

2 Answers2

1

"Dependency" can mean many different things, depending (pun intended) on the context ;)

In this case, it really means "what specific 'object' (from perhaps many) do I need to get the job done?"

For example, a component needs something that "prints". The dependency is "printing", but the requested object could print to an HP Laserjet 9200, or to an Oki dot matrix, or to .pdf file.

To put it differently, you could substitute the word "plug-in" for "dependency" here, and keep the same meaning.

'Hope that helps ..

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

When talking about dependency injection, I'd usually say that classes, not objects have dependencies.

Class A has a dependency on class B if it requires that class B exists and possibly works in a certain way. For example, if class A has a call to new B(), it has a dependency on class B. If class Bwere to disappear or change, your class A may break.

You can in some languages break dependencies by allowing the class to depend on an interface instead. If you depend on interface I instead and B implements I, B can go away and be replaced by C that also implements I, and A wouldn't need to change at all. As an example here, you could take a driver in an operating system, if you replace the disk you may get a new driver that implements the "disk drive" interface, but your operating system still talks to the disk in the same way without knowing exactly what type of disk it is.

Dependency injection is about letting you depend on interfaces instead of classes, basically instead of saying new B(), you'll just declare that you want an object that implements I and a suitable implementation will be injected for you. Your class A doesn't have to have any idea that class B or C even exist.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • If you're saying that "class A" has a "dependency" on *interface* I (which is implemented by classes B and C) then I would agree. I would go further by saying the class "publishes" a dependency by *requiring* an interface. But the concept is applicable in languages that don't even have "interfaces" in the Java sense of the word. Perhaps "protocol" and "plug-in" might be better, more generic terms for "interface" and "implementation". – paulsm4 Aug 29 '12 at 21:29
  • @paulsm4 True, requiring an interface is a better wording, especially when it's about dependency injection. I'm playing a bit loosely with "interface" here since it's a concept that is generally understandable even if it implementation wise isn't represented the same way in all languages. – Joachim Isaksson Aug 29 '12 at 21:35