14

I'm developing a simple tree-structured database and I'm usually setting dependencies or optional settings via a Builder (Builder pattern). Now I'm not sure when to use for instance Guice, when to use the Builder pattern and when to use a static factory method instead of the constructor itself. I've read Effective Java several times and I think it mentions at least a lot of advantages for not exposing the constructor. It's time to reread ;-)

So, do you know of cases which are clearly distinguishable? And shouldn't I expose the constructor? Thus for instance in every case write public static Foo getInstance(...) { return new Foo(...)}?

2
  • 2
    I suggest do what you believe is the simplest and most natural way of building your components. Sep 20, 2012 at 20:48
  • Always strive to create regular Java objects, with constructors or setters. Then, on some special occasions, consider DI to decouple implementations, or factories/builders to assist your constructional logic when needed. Sep 20, 2012 at 21:27

3 Answers 3

17

I'm a firm believer in that you don't need to use dependency injection for everything.

  • For a LookupService it would be natural inject a Dictionary such that its implementation can be swapped out by configuration.

  • For a Firewall on the other hand. It would be natural for it to create its own FireWallRules, perhaps through a supplied Factory or a Builder.

As a guideline, inject what you need to configure, don't automatically inject everything else.


Consider a static factory (*) when

  • named construction logic is desired. E.g., Lists.newArrayList()
  • the construction is so complicated it doesn't belong in the class itself
  • no configuration of the factory is required, and the factory has no side effects

Consider instance factories when

  • there is complex instantiation logic
  • configuration of the factory is needed
  • using AbstractFactory design pattern
  • there's need to create additional objects throughout the programs lifecycle

Consider a builder when

  • there are complex parameter choices. E.g., 5 parameters where some are optional.

(*) Static methods are not always testable and the presence of one should in my opinion always be motivated. A typical usecase for a factory is to decrease coupling. By using a static factory that ability is completely lost.

4
  • So maybe instead of using the AbstractFactory design pattern dependency injection frameworks are sometimes used?
    – Johannes
    Sep 20, 2012 at 21:16
  • 1
    @Johannes, In a sence. A Factory is primarily needed to create additional instances through the programs lifecycle. A Factory can still be injected by a framework through. Sep 20, 2012 at 21:19
  • @JohanSjöberg I find myself actually relying more on injected Factories when I use D.I.
    – sinθ
    Mar 24, 2014 at 23:38
  • Just a small point: In your example of a FireWallRules, there actually is an advantage to DI, since you can use a different implementation in order to automatically save them to a file and reload them automatically, for instance. For things like that, I usually use an instance factory.
    – sinθ
    Mar 24, 2014 at 23:41
13

Builder pattern vs. Dependency Injection

How are these 2 even close to comparable in your mind?
The builder pattern is used when you need to deal with classes whose constructors would have an overwhelming number of parameters (potentially optional) and this pattern makes your code easier to read and write.

Dependency Injection is an approach that facilitates loose coupling removing the dependencies of higher level classes to lower level classes. E.g. a class that needs to connect to a database does not directly create a connection but a connection is "injected" and this connection could be swapped to a different database without affecting the code using it.

1
  • 3
    Is it possible to use builder pattern and Assisted Injection together on an object creation? Apr 28, 2015 at 2:30
3

I have started using builder for most of my projects and it turns out I can and have replaced all of my DI with builders and singleton.

ie:

AppContext appContext = new AppContext.Builder()
.setProperties(testProps)
.setDB(testDB)
.build();

// run tests

My code has become much simpler to manage without DI.

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.