Dependency injection killed the factory pattern

I was having a play with Guice (http://code.google.com/p/google-guice/), the new dependency injection framework from the folks at Google, and something quite profound happened. I realised that having used Spring for a while now, it had been ages since I had to code up a factory class for pretty much anything.

No more weird JNDI lookup code, no switching in test classes if some weird ThreadLocal is set to "test". A whole huge chunk of plumbing code has been washed away. I no longer have to worry about things like whether I’m going to be running my test cases in or out of container and how stuff under the bonnet is going to do lookups in both case. I don’t care whether my JUnit/TestNG tests are being run in Eclipse or Ant. My code is cleaner, easier and faster to test, and far simpler. Any time I can’t test my code as POJOs, I feel like that dream you had in primary school where you turn up to school and you’re not wearing any pants (OK, maybe that’s just me).

So OK, your DI framework of choice still has a factory in there somewhere to auto-wire, or run off config, or annotations or whatever… but the point is "You’re Not The One Doing It" (YNTODI – original abbreviation, I checked). The less potentially error-prone code I have to write (hey, bugs are part of the job) the better.

A huge tip of the hat to the Google lads as well, the annotation based Guice looks like a really nice drop-in for the DI aspect of Spring. Keep up the good work, and putting in the hard yards so guys like me don’t have to 😉


Posted

in

, ,

by

Tags:

Comments

One response to “Dependency injection killed the factory pattern”

  1. Kevin Bourrillion Avatar
    Kevin Bourrillion

    I love this post. 🙂