2

Possible Duplicate:
What is Inversion of Control?

I'm really confused with the concept of dependency injection.

I'm very new to software field and I have a doubt on the following scenario. Let say I need a Json parser class for my java code, So I'm adding the Json jar when executing the java program using the classpath argument. My program is dependent on Json jar, so that it mean I'm doing a dependency injection here?

An another example will be, using import statement does solve the problem of user class is depending on other class ( like Date in java ) so all these are the concept of dependency injection?

Where I'm making the concept.

Thanks in advance.

Community
  • 1
  • 1
sriram
  • 8,562
  • 19
  • 63
  • 82
  • This is not dependency injection at all. Google IoC (Inversion of Control) to get a better feel for it. – Marthin Dec 05 '12 at 11:47
  • No, neither of these is dependency injection; these are just dependencies. Look up "Inversion of Control" to read upon the subject. – Sergey Kalinichenko Dec 05 '12 at 11:48
  • @assylias: I don't think so. I was confused the DI concept with others. And I didn't ask what it is actually. – sriram Dec 05 '12 at 11:57

5 Answers5

8

It's much more to do with how you link your components together. e.g. in the above, I would expect you to inject your parser into the class that needs it. e.g.

Instead of:

public class MyParserUsingClass {
   ...
   public MyParserUsingClass() {
      this.parser = new Parser();
   }
}

you would do:

public class MyParserUsingClass {
   ...
   public MyParserUsingClass(Parser injectedParser) {
      this.parser = injectedParser;
   }
}

Why do this ? Your class using the parser doesn't really care where the parser comes from and should really use an interface rather than a concrete instance. By injecting the parser, you can supply different instances depending on circumstances, mock it out for testing etc. Otherwise the class would just create it internally and you'd have no control over it. This is particularly key for configurable components, components that talk across the network, heavyweight components etc.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

In your specific example, dependency injection would look like:

public class MyParser {
     private final JsonParser parser;

     public MyParser(JsonParser parser) {
         this.parser = parser;
     }

     public Result parse(JsonInput input) {
         Result = parser.parse(input);
     }
}

Assuming that JsonParser is an interface, you can now provide any implementation of that interface to MyParser which does not depend on which specific implementation you choose to use.

assylias
  • 321,522
  • 82
  • 660
  • 783
1

Let's say you have a class the daes the job called MyCustomParser . Now you want to allow to inject the parser in that class to obtain a different behavior against the type of Parser you will inject (the json could change,you would like to use another way to parse etc). So you should create an Interface - IParser with a method called (by instance) ParseIt Now you can have different implementation of this method depending on how you want to do the parsing. All you need to do is to pass this Interface to the class that will use it MyCustomParser (you might do that passing it as parameter in the constructor). In this case you are injecting the Parser in the MyCustomParser class

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
1

Dependency injection, in the strict sense, has nothing to do with importing classes or adding jar files to your classpath. It is meant to support the programming to interfaces principle and it's a form of Inversion of Control.

The main benefit it brings is that your code does not depend on explicit dependency implementation, but it uses abstractions instead. You depend on an interface and some instance of an implementing class for that interface will be "injected" in your class at runtime (either as a constructor parameter, some setter, or -- bad choice, in my opinion -- instance field).

As a consequence, you can for example change your persistence layer without touching the code in your service components, because your service classes only care about some DAO/Repository interfaces and you'll provide them with different implementations under the hood.

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
0

Dependency injection is more about reducing the instances where a class has to create instances of things it depends upon for itself.

Dependency injection aims to instead 'give' those dependencies to the consuming class via some external mechanism. Common methods include passing the dependencies in via the constructor, or setting them into a publicly 'settable' property of the class.

For example, dependency injection via the constructor could look something like this:

public class Parser
{
     private IJsonParser parser;

     public Parser(IJsonParser parser) {
         this.parser = parser;
     }
}

Which removes the need for the Parser class to do something like this:

public class Parser
{
     private IJsonParser parser;

     public Parser() {
         this.parser = new JsonParser();
     }
}

This follows from the principles of single responsibility - the Parser class doesn't need to be responsible for creating a JsonParser, nor should it care what specific JsonParser it uses - its concern is simply that it requires something that does the work specified by the IJsonParser interface, and leave it up to some higher controlling code to determine the appropriate concrete type that is best suited to doing that work.

Chamila Chulatunga
  • 4,856
  • 15
  • 17