Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Besides low coupling, that others have already touched on, a major difference is that with the <code>new</code> approach, <strong>you get a new object every time</strong>, whether you want to or not. Even if <code>UserDaoImpl</code> is reusable, stateless and threadsafe (which DAO classes should strive to be) you will still create new instances of them at every place they are needed. </p> <p>This may not be a huge issue at first, but consider as the object graph grows - the <code>UserDaoImpl</code> perhaps needs a Hibernate session, which needs a DataSource, which needs a JDBC connection - it quickly becomes a lot of objects that has to be created and initialized over and over again. When you rely on <code>new</code> in your code, you are also spreading out initialization logic over a whole bunch of places. Like in this example, you need to have code in your UserDaoImpl to open a JDBC connection with the proper parameters, but all other DAO classes have to do the same thing.</p> <p>And here is where Inversion-of-Control (IoC) comes in. It aims to address just these things, by decoupling object creation and life-cycle from object binding and usage. The most basic application of IoC is a simple Factory class. A more sophisticated approach is Dependency Injection, such as Spring.</p> <p><strong>Is there an impact on the performance?</strong></p> <p>Yes, but it's most likely not going to be very significant. Using Springs dependency injection costs a little more in startup-time as the container has to be initialized and all managed objects set up. However, since you won't be creating new instances of your managed objects (assuming that is how you design them), you'll gain some runtime performance from less GC load and less object creation.</p> <p>Your big gain however is going to be in maintainability and robustness of your application.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload