Using patterns to change dependency injection styles
The two most common forms of dependency injection are constructor injection and setter injection. They’ve got pros and cons of course, and both have their place. Setter injection is simple, allows you choose which parts of the object to inject, can make setting up bidirectional relationships easier, but has the downside that it’s easier to create an object in a partially constructed state. Constructor injection allows more control over the instantiation of a valid object but can lead to a bunch of constructors and/or the use of lots of optional arguments.
But this post is really more about how you can use the Builder and Factory Method patterns to effectively choose between setter injection and constructor injection.
For instance, consider an object defined with constructor injection:
public class Foo {
private A;
private B;
private C;
public Foo(A a, B b, C c) {
this.a = a;
this.b = b;
this.c = c;
}
}
But say that b and c are optional (and perhaps we also have d, e, and f). So instead, we create a Builder object:
public class FooBuilder {
private A a;
private B b;
private C c;
public void setA(A a) { this.a = a; }
public void setB(B b) { this.b = b; }
public void setC(C c) { this.c = c; }
public Foo build() { return new Foo(a, b, c); }
}
So, if you look at the use of FooBuilder vs the use of Foo, you’ll see that we have effectively turned constructor injection into setter injection.
Similarly, given a class that uses setter injection, you can turn that into constructor injection style by using one or more Factory methods that correspond to the constructors in constructor injection:
public class Foo {
private A a;
private B b;
private C c;
public void setA(A a) { this.a = a; }
public void setB(B b) { this.b = b; }
public void setC(C c) { this.c = c; }
public Foo() { }
}
public class FooFactory {
public Foo newFoo(A a, C c) {
Foo foo = new Foo();
foo.setA(a);
foo.setC(c);
return foo;
}
}
Just a thought….

Hi! My name is Alex Miller and I live in St. Louis. I write code for a living and currently work for
One thing I like in constructor injection is that we can make the instance variables final.
I’d make one change to the FooBuilder – I’d make the setter methods return FooBuilder… Makes it a little easier to build Foo:
FooBuilder fb = new FooBuilder();
Foo f = fb.setA(a).setB(b).build();
Yeah, that’s a common style. I’m still on the fence about it so I left it out. You see this a lot in fluent interfaces. There was actually some discussion of actually this with regard to ProcessBuilder in the JDK on the recent Java Posse #135.
You might also be interested in Matthias Ernst’s proposal to cascade void functions which would actually give the ability to chain calls automatically by having all void methods implicitly return this. He’s got a patch you can actually try it out if you want.