Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between creating new object and dependency injection

What is the difference between creating a new object and dependency injection? Please explain in detail.

like image 471
TaherT Avatar asked Aug 02 '10 10:08

TaherT


People also ask

Does dependency injection create a new object?

Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

How object is create in dependency injection?

Dependency injection is the principle that the objects which a class needs should be 'injected' into it, not created inside it. Injection usually happens through the constructor, where a class receives any objects it needs as constructor parameters.

What is difference between dependency injection and factory design pattern?

Dependency Injection is more of a architectural pattern for loosely coupling software components. Factory pattern is just one way to separate the responsibility of creating objects of other classes to another entity. Factory pattern can be called as a tool to implement DI.

What is the difference between dependency injection and inheritance?

One criticism of inheritance is that it tightly couples parent class with child class. It is harder to reuse the code and write unit tests. That's why most developers prefer dependency injection as a way to reuse code. Dependency injection is a way to inject dependencies into a class for use in its methods.


1 Answers

Well, they're not exactly comparable. You will always have to create a new object by instantiating a class at some point. Dependency injection also requires creating new objects.

Dependency injection really comes into play when you want to control or verify the behavior of instances used by a class that you use or want to test. (For Test Driven Development, dependency injection is key for all but the smallest example).

Assume a class Holder which requires an object of class Handle. The traditional way to do that would be to let the Holder instance create and own it:

class Holder {     private Handle myHandle = new Handle();     public void handleIt() {         handle.handleIt();     } } 

The Holder instance creates myHandle and no one outside the class can get at it. In some cases, unit-testing being one of them, this is a problem because it is not possible to test the Holder class without creating the Handle instance which in turn might depend on many other classes and instances. This makes testing unwieldy and cumbersome.

By injecting the Handle instance, for example in the constructor, someone from the outside becomes responsible for the creation of the instance.

class Holder {     private Handle myHandle;      public Holder(Handle injectedHandle) {         myHandle = injectedHandle;     }      public void handleIt() {         handle.handleIt();     } } 

As you can see the code is almost the same, and the Handle is still private, but the Holder class now has a much loser coupling to its outside world which makes many things simpler. And when testing the Holder class a mock or stub object can be injected instead of a real instance making it possible to verify or control the interaction between the Holder, its caller and the handle.

The actual injection would take place at some other place, usually some "main" program. There are multiple frameworks that can help you do that without programming, but essentially this is the code in the "main" program:

... private Handle myHandle = new Handle(); // Create the instance to inject private Handler theHandler = new Handler(myHandle); // Inject the handle ... 

In essence, the injection is nothing more than a fancy set method. And of course, you can implement the injection mechanism using that instead of in the constructor like the simple example above.

like image 136
thoni56 Avatar answered Sep 19 '22 17:09

thoni56